@@ -199,7 +199,6 @@ def __init__(
199
199
pool .bind (self )
200
200
self ._session_manager = DatabaseSessionsManager (database = self , pool = pool )
201
201
202
-
203
202
@classmethod
204
203
def from_pb (cls , database_pb , instance , pool = None ):
205
204
"""Creates an instance of this class from a protobuf.
@@ -785,8 +784,8 @@ def execute_pdml():
785
784
# re-raising the error.
786
785
except NotImplementedError as exc :
787
786
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 )
790
789
):
791
790
self .session_options .disable_multiplexed (
792
791
self .logger , TransactionType .PARTITIONED
@@ -1248,8 +1247,20 @@ def observability_options(self):
1248
1247
1249
1248
class SessionCheckout (object ):
1250
1249
"""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
+
1251
1257
:type database: :class:`~google.cloud.spanner_v1.database.Database`
1252
1258
: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.
1253
1264
"""
1254
1265
1255
1266
_session = None # Not checked out until '__enter__'.
@@ -1261,6 +1272,7 @@ def __init__(
1261
1272
):
1262
1273
# Move import here to avoid circular import
1263
1274
from google .cloud .spanner_v1 .database import Database
1275
+
1264
1276
if not isinstance (database , Database ):
1265
1277
raise TypeError (
1266
1278
"{class_name} must receive an instance of {expected_class_name}. Received: {actual_class_name}" .format (
@@ -1290,6 +1302,7 @@ def __enter__(self):
1290
1302
def __exit__ (self , * ignored ):
1291
1303
self ._database ._session_manager .put_session (self ._session )
1292
1304
1305
+
1293
1306
class BatchCheckout (object ):
1294
1307
"""Context manager for using a batch from a database.
1295
1308
@@ -1339,8 +1352,12 @@ def __init__(
1339
1352
1340
1353
def __enter__ (self ):
1341
1354
"""Begin ``with`` block."""
1355
+ from google .cloud .spanner_v1 .database import TransactionType
1356
+
1342
1357
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
+ )
1344
1361
add_span_event (current_span , "Using session" , {"id" : session .session_id })
1345
1362
batch = self ._batch = Batch (session )
1346
1363
if self ._request_options .transaction_tag :
@@ -1365,7 +1382,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
1365
1382
"CommitStats: {}" .format (self ._batch .commit_stats ),
1366
1383
extra = {"commit_stats" : self ._batch .commit_stats },
1367
1384
)
1368
- self ._database ._pool . put (self ._session )
1385
+ self ._database ._session_manager . put_session (self ._session )
1369
1386
current_span = get_current_span ()
1370
1387
add_span_event (
1371
1388
current_span ,
@@ -1393,7 +1410,11 @@ def __init__(self, database):
1393
1410
1394
1411
def __enter__ (self ):
1395
1412
"""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
+ )
1397
1418
return MutationGroups (session )
1398
1419
1399
1420
def __exit__ (self , exc_type , exc_val , exc_tb ):
@@ -1402,9 +1423,9 @@ def __exit__(self, exc_type, exc_val, exc_tb):
1402
1423
# If NotFound exception occurs inside the with block
1403
1424
# then we validate if the session still exists.
1404
1425
if not self ._session .exists ():
1405
- self ._session = self ._database ._pool ._new_session ()
1426
+ self ._session = self ._database ._session_manager . _pool ._new_session ()
1406
1427
self ._session .create ()
1407
- self ._database ._pool . put (self ._session )
1428
+ self ._database ._session_manager . put_session (self ._session )
1408
1429
1409
1430
1410
1431
class SnapshotCheckout (object ):
@@ -1432,7 +1453,11 @@ def __init__(self, database, **kw):
1432
1453
1433
1454
def __enter__ (self ):
1434
1455
"""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
+ )
1436
1461
return Snapshot (session , ** self ._kw )
1437
1462
1438
1463
def __exit__ (self , exc_type , exc_val , exc_tb ):
@@ -1441,9 +1466,9 @@ def __exit__(self, exc_type, exc_val, exc_tb):
1441
1466
# If NotFound exception occurs inside the with block
1442
1467
# then we validate if the session still exists.
1443
1468
if not self ._session .exists ():
1444
- self ._session = self ._database ._pool ._new_session ()
1469
+ self ._session = self ._database ._session_manager . _pool ._new_session ()
1445
1470
self ._session .create ()
1446
- self ._database ._pool . put (self ._session )
1471
+ self ._database ._session_manager . put_session (self ._session )
1447
1472
1448
1473
1449
1474
class BatchSnapshot (object ):
@@ -1488,8 +1513,12 @@ def from_dict(cls, database, mapping):
1488
1513
1489
1514
:rtype: :class:`BatchSnapshot`
1490
1515
"""
1516
+ from google .cloud .spanner_v1 .database import TransactionType
1517
+
1491
1518
instance = cls (database )
1492
- session = instance ._session = database .session ()
1519
+ session = instance ._session = database ._session_manager .get_session (
1520
+ TransactionType .READ_ONLY
1521
+ )
1493
1522
session ._session_id = mapping ["session_id" ]
1494
1523
snapshot = instance ._snapshot = session .snapshot ()
1495
1524
snapshot ._transaction_id = mapping ["transaction_id" ]
@@ -1522,8 +1551,12 @@ def _get_session(self):
1522
1551
Caller is responsible for cleaning up the session after
1523
1552
all partitions have been processed.
1524
1553
"""
1554
+ from google .cloud .spanner_v1 .database import TransactionType
1555
+
1525
1556
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
+ )
1527
1560
if self ._session_id is None :
1528
1561
session .create ()
1529
1562
else :
@@ -1978,4 +2011,3 @@ def _retry_on_aborted(func, retry_config):
1978
2011
"""
1979
2012
retry = retry_config .with_predicate (if_exception_type (Aborted ))
1980
2013
return retry (func )
1981
-
0 commit comments