This repository was archived by the owner on Mar 31, 2026. It is now read-only.
Commit f176484
committed
feat(dbapi): use inline begin in transaction_checkout to eliminate BeginTransaction RPC
Remove the explicit `self._transaction.begin()` call from
`Connection.transaction_checkout()`. The transaction is now returned
with `_transaction_id=None`, allowing `Transaction.execute_sql()`,
`execute_update()`, and `batch_update()` to use their existing
inline begin logic via `TransactionSelector(begin=...)`.
This piggybacks the BeginTransaction onto the first ExecuteSql RPC,
eliminating one round-trip per transaction (~16ms on the emulator).
The inline begin code path already exists in the Transaction class
and is used by `Session.run_in_transaction()`. This change makes the
DBAPI layer use the same optimization.
Safety:
- `transaction_checkout()` is only called from `run_statement()` and
`batch_dml_executor`, both of which immediately execute SQL, so
`_transaction_id` is always set before commit/rollback.
- `Transaction.rollback()` already handles `_transaction_id is None`
(skips the RPC — correct when no server-side txn exists).
- `Transaction.commit()` handles `_transaction_id is None` for
mutation-only transactions via `_begin_mutations_only_transaction()`.
- The retry mechanism (`_set_connection_for_retry`) resets
`_spanner_transaction_started=False`, so replayed statements
create a fresh Transaction and use inline begin again.
All 198 DBAPI unit tests and 53 system tests (against the Spanner
emulator) pass with this change.
Made-with: Cursor1 parent 12773d7 commit f176484
File tree
2 files changed
+28
-2
lines changed- google/cloud/spanner_dbapi
- tests/unit/spanner_dbapi
2 files changed
+28
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
392 | 392 | | |
393 | 393 | | |
394 | 394 | | |
395 | | - | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
396 | 403 | | |
397 | 404 | | |
398 | 405 | | |
| |||
410 | 417 | | |
411 | 418 | | |
412 | 419 | | |
413 | | - | |
414 | 420 | | |
415 | 421 | | |
416 | 422 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
211 | 211 | | |
212 | 212 | | |
213 | 213 | | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
214 | 234 | | |
215 | 235 | | |
216 | 236 | | |
| |||
0 commit comments