Skip to content

Commit

Permalink
Block creation of transaction for session w/ existing txn. (googleapi…
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver authored and landrito committed Aug 21, 2017
1 parent 61afd42 commit 71d48d6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions spanner/google/cloud/spanner/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ def transaction(self):

if self._transaction is not None:
self._transaction._rolled_back = True
del self._transaction

txn = self._transaction = Transaction(self)
return txn
Expand Down
14 changes: 13 additions & 1 deletion spanner/google/cloud/spanner/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,24 @@


class Transaction(_SnapshotBase, _BatchBase):
"""Implement read-write transaction semantics for a session."""
"""Implement read-write transaction semantics for a session.
:type session: :class:`~google.cloud.spanner.session.Session`
:param session: the session used to perform the commit
:raises ValueError: if session has an existing transaction
"""
committed = None
"""Timestamp at which the transaction was successfully committed."""
_rolled_back = False
_multi_use = True

def __init__(self, session):
if session._transaction is not None:
raise ValueError("Session has existing transaction.")

super(Transaction, self).__init__(session)

def _check_state(self):
"""Helper for :meth:`commit` et al.
Expand Down
6 changes: 6 additions & 0 deletions spanner/tests/unit/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ def _make_one(self, session, *args, **kwargs):
session._transaction = transaction
return transaction

def test_ctor_session_w_existing_txn(self):
session = _Session()
session._transaction = object()
with self.assertRaises(ValueError):
transaction = self._make_one(session)

def test_ctor_defaults(self):
session = _Session()
transaction = self._make_one(session)
Expand Down

0 comments on commit 71d48d6

Please sign in to comment.