107107from pymongo .synchronous import client_session , database , uri_parser
108108from pymongo .synchronous .change_stream import ChangeStream , ClusterChangeStream
109109from pymongo .synchronous .client_bulk import _ClientBulk
110- from pymongo .synchronous .client_session import _EmptyServerSession
110+ from pymongo .synchronous .client_session import SESSION , _EmptyServerSession
111111from pymongo .synchronous .command_cursor import CommandCursor
112112from pymongo .synchronous .settings import TopologySettings
113113from pymongo .synchronous .topology import Topology , _ErrorContext
@@ -1353,13 +1353,18 @@ def _close_cursor_soon(
13531353 def _start_session (self , implicit : bool , ** kwargs : Any ) -> ClientSession :
13541354 server_session = _EmptyServerSession ()
13551355 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
13571361
13581362 def start_session (
13591363 self ,
13601364 causal_consistency : Optional [bool ] = None ,
13611365 default_transaction_options : Optional [client_session .TransactionOptions ] = None ,
13621366 snapshot : Optional [bool ] = False ,
1367+ bind : Optional [bool ] = False ,
13631368 ) -> client_session .ClientSession :
13641369 """Start a logical session.
13651370
@@ -1382,6 +1387,7 @@ def start_session(
13821387 causal_consistency = causal_consistency ,
13831388 default_transaction_options = default_transaction_options ,
13841389 snapshot = snapshot ,
1390+ bind = bind ,
13851391 )
13861392
13871393 def _ensure_session (self , session : Optional [ClientSession ] = None ) -> Optional [ClientSession ]:
0 commit comments