107
107
from pymongo .synchronous import client_session , database , uri_parser
108
108
from pymongo .synchronous .change_stream import ChangeStream , ClusterChangeStream
109
109
from pymongo .synchronous .client_bulk import _ClientBulk
110
- from pymongo .synchronous .client_session import _EmptyServerSession
110
+ from pymongo .synchronous .client_session import SESSION , _EmptyServerSession
111
111
from pymongo .synchronous .command_cursor import CommandCursor
112
112
from pymongo .synchronous .settings import TopologySettings
113
113
from pymongo .synchronous .topology import Topology , _ErrorContext
@@ -1353,13 +1353,18 @@ def _close_cursor_soon(
1353
1353
def _start_session (self , implicit : bool , ** kwargs : Any ) -> ClientSession :
1354
1354
server_session = _EmptyServerSession ()
1355
1355
opts = client_session .SessionOptions (** kwargs )
1356
- return client_session .ClientSession (self , server_session , opts , implicit )
1356
+ bind = opts ._bind
1357
+ session = client_session .ClientSession (self , server_session , opts , implicit )
1358
+ if bind :
1359
+ SESSION .set (session )
1360
+ return session
1357
1361
1358
1362
def start_session (
1359
1363
self ,
1360
1364
causal_consistency : Optional [bool ] = None ,
1361
1365
default_transaction_options : Optional [client_session .TransactionOptions ] = None ,
1362
1366
snapshot : Optional [bool ] = False ,
1367
+ bind : Optional [bool ] = False ,
1363
1368
) -> client_session .ClientSession :
1364
1369
"""Start a logical session.
1365
1370
@@ -1382,6 +1387,7 @@ def start_session(
1382
1387
causal_consistency = causal_consistency ,
1383
1388
default_transaction_options = default_transaction_options ,
1384
1389
snapshot = snapshot ,
1390
+ bind = bind ,
1385
1391
)
1386
1392
1387
1393
def _ensure_session (self , session : Optional [ClientSession ] = None ) -> Optional [ClientSession ]:
0 commit comments