Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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