Skip to content

Commit fc55e91

Browse files
committed
Inline Begin transction for RW transactions
1 parent 06725fc commit fc55e91

File tree

2 files changed

+501
-4
lines changed

2 files changed

+501
-4
lines changed

google/cloud/spanner_v1/transaction.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ def _check_state(self):
6161
:raises: :exc:`ValueError` if the object's state is invalid for making
6262
API requests.
6363
"""
64-
if self._transaction_id is None:
65-
raise ValueError("Transaction is not begun")
66-
64+
6765
if self.committed is not None:
6866
raise ValueError("Transaction is already committed")
6967

@@ -78,7 +76,11 @@ def _make_txn_selector(self):
7876
:returns: a selector configured for read-write transaction semantics.
7977
"""
8078
self._check_state()
81-
return TransactionSelector(id=self._transaction_id)
79+
80+
if self._transaction_id is None:
81+
return TransactionSelector(begin=TransactionOptions(read_write=TransactionOptions.ReadWrite()))
82+
else:
83+
return TransactionSelector(id=self._transaction_id)
8284

8385
def begin(self):
8486
"""Begin a transaction on the database.
@@ -302,6 +304,10 @@ def execute_update(
302304
response = api.execute_sql(
303305
request=request, metadata=metadata, retry=retry, timeout=timeout
304306
)
307+
308+
if self._transaction_id is None and response.metadata.transaction is not None:
309+
self._transaction_id = response.metadata.transaction.id
310+
305311
return response.stats.row_count_exact
306312

307313
def batch_update(self, statements, request_options=None):

0 commit comments

Comments
 (0)