@@ -199,7 +199,6 @@ def __init__(
199199 pool .bind (self )
200200 self ._session_manager = DatabaseSessionsManager (database = self , pool = pool )
201201
202-
203202 @classmethod
204203 def from_pb (cls , database_pb , instance , pool = None ):
205204 """Creates an instance of this class from a protobuf.
@@ -785,8 +784,8 @@ def execute_pdml():
785784 # re-raising the error.
786785 except NotImplementedError as exc :
787786 if (
788- "Transaction type partitioned_dml not supported with multiplexed sessions"
789- in str (exc )
787+ "Transaction type partitioned_dml not supported with multiplexed sessions"
788+ in str (exc )
790789 ):
791790 self .session_options .disable_multiplexed (
792791 self .logger , TransactionType .PARTITIONED
@@ -1248,8 +1247,20 @@ def observability_options(self):
12481247
12491248class SessionCheckout (object ):
12501249 """Context manager for using a session from a database.
1250+
1251+ This is the recommended way to obtain sessions for database operations.
1252+ It automatically integrates with the database session manager to support
1253+ multiplexed sessions when enabled via environment variables.
1254+
1255+ For read-only operations, consider using ``database.snapshot()`` instead.
1256+
12511257 :type database: :class:`~google.cloud.spanner_v1.database.Database`
12521258 :param database: database to use the session from
1259+
1260+ :type transaction_type: :class:`~google.cloud.spanner_v1.session_options.TransactionType`
1261+ :param transaction_type: type of transaction this session will be used for.
1262+ Defaults to READ_WRITE. For read-only operations, use READ_ONLY to
1263+ enable multiplexed session support.
12531264 """
12541265
12551266 _session = None # Not checked out until '__enter__'.
@@ -1261,6 +1272,7 @@ def __init__(
12611272 ):
12621273 # Move import here to avoid circular import
12631274 from google .cloud .spanner_v1 .database import Database
1275+
12641276 if not isinstance (database , Database ):
12651277 raise TypeError (
12661278 "{class_name} must receive an instance of {expected_class_name}. Received: {actual_class_name}" .format (
@@ -1290,6 +1302,7 @@ def __enter__(self):
12901302 def __exit__ (self , * ignored ):
12911303 self ._database ._session_manager .put_session (self ._session )
12921304
1305+
12931306class BatchCheckout (object ):
12941307 """Context manager for using a batch from a database.
12951308
@@ -1339,8 +1352,12 @@ def __init__(
13391352
13401353 def __enter__ (self ):
13411354 """Begin ``with`` block."""
1355+ from google .cloud .spanner_v1 .database import TransactionType
1356+
13421357 current_span = get_current_span ()
1343- session = self ._session = self ._database ._pool .get ()
1358+ session = self ._session = self ._database ._session_manager .get_session (
1359+ TransactionType .READ_WRITE
1360+ )
13441361 add_span_event (current_span , "Using session" , {"id" : session .session_id })
13451362 batch = self ._batch = Batch (session )
13461363 if self ._request_options .transaction_tag :
@@ -1365,7 +1382,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
13651382 "CommitStats: {}" .format (self ._batch .commit_stats ),
13661383 extra = {"commit_stats" : self ._batch .commit_stats },
13671384 )
1368- self ._database ._pool . put (self ._session )
1385+ self ._database ._session_manager . put_session (self ._session )
13691386 current_span = get_current_span ()
13701387 add_span_event (
13711388 current_span ,
@@ -1393,7 +1410,11 @@ def __init__(self, database):
13931410
13941411 def __enter__ (self ):
13951412 """Begin ``with`` block."""
1396- session = self ._session = self ._database ._pool .get ()
1413+ from google .cloud .spanner_v1 .database import TransactionType
1414+
1415+ session = self ._session = self ._database ._session_manager .get_session (
1416+ TransactionType .READ_WRITE
1417+ )
13971418 return MutationGroups (session )
13981419
13991420 def __exit__ (self , exc_type , exc_val , exc_tb ):
@@ -1402,9 +1423,9 @@ def __exit__(self, exc_type, exc_val, exc_tb):
14021423 # If NotFound exception occurs inside the with block
14031424 # then we validate if the session still exists.
14041425 if not self ._session .exists ():
1405- self ._session = self ._database ._pool ._new_session ()
1426+ self ._session = self ._database ._session_manager . _pool ._new_session ()
14061427 self ._session .create ()
1407- self ._database ._pool . put (self ._session )
1428+ self ._database ._session_manager . put_session (self ._session )
14081429
14091430
14101431class SnapshotCheckout (object ):
@@ -1432,7 +1453,11 @@ def __init__(self, database, **kw):
14321453
14331454 def __enter__ (self ):
14341455 """Begin ``with`` block."""
1435- session = self ._session = self ._database ._pool .get ()
1456+ from google .cloud .spanner_v1 .database import TransactionType
1457+
1458+ session = self ._session = self ._database ._session_manager .get_session (
1459+ TransactionType .READ_ONLY
1460+ )
14361461 return Snapshot (session , ** self ._kw )
14371462
14381463 def __exit__ (self , exc_type , exc_val , exc_tb ):
@@ -1441,9 +1466,9 @@ def __exit__(self, exc_type, exc_val, exc_tb):
14411466 # If NotFound exception occurs inside the with block
14421467 # then we validate if the session still exists.
14431468 if not self ._session .exists ():
1444- self ._session = self ._database ._pool ._new_session ()
1469+ self ._session = self ._database ._session_manager . _pool ._new_session ()
14451470 self ._session .create ()
1446- self ._database ._pool . put (self ._session )
1471+ self ._database ._session_manager . put_session (self ._session )
14471472
14481473
14491474class BatchSnapshot (object ):
@@ -1488,8 +1513,12 @@ def from_dict(cls, database, mapping):
14881513
14891514 :rtype: :class:`BatchSnapshot`
14901515 """
1516+ from google .cloud .spanner_v1 .database import TransactionType
1517+
14911518 instance = cls (database )
1492- session = instance ._session = database .session ()
1519+ session = instance ._session = database ._session_manager .get_session (
1520+ TransactionType .READ_ONLY
1521+ )
14931522 session ._session_id = mapping ["session_id" ]
14941523 snapshot = instance ._snapshot = session .snapshot ()
14951524 snapshot ._transaction_id = mapping ["transaction_id" ]
@@ -1522,8 +1551,12 @@ def _get_session(self):
15221551 Caller is responsible for cleaning up the session after
15231552 all partitions have been processed.
15241553 """
1554+ from google .cloud .spanner_v1 .database import TransactionType
1555+
15251556 if self ._session is None :
1526- session = self ._session = self ._database .session ()
1557+ session = self ._session = self ._database ._session_manager .get_session (
1558+ TransactionType .READ_ONLY
1559+ )
15271560 if self ._session_id is None :
15281561 session .create ()
15291562 else :
@@ -1978,4 +2011,3 @@ def _retry_on_aborted(func, retry_config):
19782011 """
19792012 retry = retry_config .with_predicate (if_exception_type (Aborted ))
19802013 return retry (func )
1981-
0 commit comments