Skip to content

Commit 549aadb

Browse files
tseaverlandrito
authored andcommitted
Block creation of transaction for session w/ existing txn. (googleapis#3785)
Closes googleapis#3476.
1 parent 8bca9ee commit 549aadb

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

spanner/google/cloud/spanner/session.py

+1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ def transaction(self):
249249

250250
if self._transaction is not None:
251251
self._transaction._rolled_back = True
252+
del self._transaction
252253

253254
txn = self._transaction = Transaction(self)
254255
return txn

spanner/google/cloud/spanner/transaction.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,24 @@
2424

2525

2626
class Transaction(_SnapshotBase, _BatchBase):
27-
"""Implement read-write transaction semantics for a session."""
27+
"""Implement read-write transaction semantics for a session.
28+
29+
:type session: :class:`~google.cloud.spanner.session.Session`
30+
:param session: the session used to perform the commit
31+
32+
:raises ValueError: if session has an existing transaction
33+
"""
2834
committed = None
2935
"""Timestamp at which the transaction was successfully committed."""
3036
_rolled_back = False
3137
_multi_use = True
3238

39+
def __init__(self, session):
40+
if session._transaction is not None:
41+
raise ValueError("Session has existing transaction.")
42+
43+
super(Transaction, self).__init__(session)
44+
3345
def _check_state(self):
3446
"""Helper for :meth:`commit` et al.
3547

spanner/tests/unit/test_transaction.py

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ def _make_one(self, session, *args, **kwargs):
4747
session._transaction = transaction
4848
return transaction
4949

50+
def test_ctor_session_w_existing_txn(self):
51+
session = _Session()
52+
session._transaction = object()
53+
with self.assertRaises(ValueError):
54+
transaction = self._make_one(session)
55+
5056
def test_ctor_defaults(self):
5157
session = _Session()
5258
transaction = self._make_one(session)

0 commit comments

Comments
 (0)