Skip to content

Commit f6dab41

Browse files
authored
Ensure 'session._transaction' is cleared after 'run_in_transaction' succeeds (googleapis#3437)
* Ensure 'session._transaction' is cleared after 'run_in_transaction' succeeds. Closes googleapis#3434.
1 parent 937165e commit f6dab41

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

spanner/google/cloud/spanner/session.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,9 @@ def run_in_transaction(self, func, *args, **kw):
311311
_delay_until_retry(exc, deadline)
312312
del self._transaction
313313
else:
314-
return txn.committed
314+
committed = txn.committed
315+
del self._transaction
316+
return committed
315317

316318

317319
# pylint: disable=misplaced-bare-raise

spanner/tests/system/test_system.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,23 @@ def _unit_of_work(transaction, test):
323323
rows = list(self._db.execute_sql(self.SQL))
324324
self._check_row_data(rows)
325325

326+
def test_db_run_in_transaction_twice(self):
327+
retry = RetryInstanceState(_has_all_ddl)
328+
retry(self._db.reload)()
329+
330+
with self._db.batch() as batch:
331+
batch.delete(self.TABLE, self.ALL)
332+
333+
def _unit_of_work(transaction, test):
334+
transaction.insert_or_update(
335+
test.TABLE, test.COLUMNS, test.ROW_DATA)
336+
337+
self._db.run_in_transaction(_unit_of_work, test=self)
338+
self._db.run_in_transaction(_unit_of_work, test=self)
339+
340+
rows = list(self._db.execute_sql(self.SQL))
341+
self._check_row_data(rows)
342+
326343

327344
class TestSessionAPI(unittest.TestCase, _TestData):
328345
ALL_TYPES_TABLE = 'all_types'

spanner/tests/unit/test_session.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ def unit_of_work(txn, *args, **kw):
503503
unit_of_work, 'abc', some_arg='def')
504504

505505
self.assertEqual(committed, now)
506+
self.assertIsNone(session._transaction)
506507
self.assertEqual(len(called_with), 1)
507508
txn, args, kw = called_with[0]
508509
self.assertIsInstance(txn, Transaction)

0 commit comments

Comments
 (0)