Repository navigation
Conversation
When autocommit was enabled and a transaction was explicitly started with `begin()`, retrying an aborted transaction cleared the connection's explicit-transaction flag. As a result, replayed and subsequent statements fell back to autocommit mode instead of executing atomically, and `commit()` or `rollback()` became no-ops. This change fixes that by: - Keeping the explicit-transaction state active across retry attempts until the transaction is committed or rolled back. - Skipping retry recording for successful `BEGIN`, `COMMIT`, and `ROLLBACK` SQL statements (and failed `COMMIT` or `ROLLBACK` calls), so `BEGIN` is not re-executed on an already-started transaction during replay and `COMMIT`/`ROLLBACK` cannot leak into the next transaction's retry history. Failed `BEGIN` calls are still recorded so retries verify the same error. - Clearing the cursor's previous parsed statement before parsing a new one, so a statement parsing failure cannot inherit the prior statement's retry rules.
There was a problem hiding this comment.
Code Review
This pull request refactors the Spanner DB-API transaction retry logic to correctly handle client-side transaction boundary statements (such as BEGIN, COMMIT, and ROLLBACK) and preserve client-side transaction state across retry attempts. This is achieved by introducing properties on ClientSideStatementType to determine if statements should be recorded for retry, adding a helper method _should_record_execute_for_retry in Cursor, and updating _set_connection_for_retry in TransactionHelper. Extensive unit and mock server tests have been added to verify these behaviors. I have no feedback to provide as there are no review comments.
| def should_record_success_for_retry(self) -> bool: | ||
| """Whether a successful statement should be recorded for transaction retry.""" | ||
| return self not in ( | ||
| ClientSideStatementType.BEGIN, |
There was a problem hiding this comment.
Should we include SHOW_COMMIT_TIMESTAMP and SHOW_READ_TIMESTAMP because they are executed based on the results of prev commit?
For example
// Executes Transaction 1 and commit
cursor.execute("INSERT INTO singers (id, name) VALUES (1, 'A')")
conn.commit()
// Post commit - Print the commit timestamp
cursor.execute("SHOW VARIABLE COMMIT_TIMESTAMP")
rows = cursor.fetchall()
// Execute Transaction 28
cursor.execute("INSERT INTO singers (id, name) VALUES (2, 'B')")
conn.commit() -> Fails with aborted error
When seconds transaction fails with ABORTED error, reply will fail because second transaction detail is overridden because Transaction 2 is already started.
There was a problem hiding this comment.
No, we should not touch those (and also not the other SHOW / SET / START BATCH commands). This pull request intentionally limits the scope to the transaction control statements. Fixing this type of issues for connection state in general with a few small changes is not possible. Instead, the long-term fix is to replace the current ad-hoc fields that are used for connection state with a real ConnectionState implementation like we have in Java Connection API and Go database/sql. This will also make it possible to make ConnectionState transactional. I will be working on that in later pull requests.
When autocommit was enabled and a transaction was explicitly started with
begin(), retrying an aborted transaction cleared the connection's explicit-transaction flag. As a result, replayed and subsequent statements fell back to autocommit mode instead of executing atomically, andcommit()orrollback()became no-ops.This change fixes that by:
BEGIN,COMMIT, andROLLBACKSQL statements (and failedCOMMITorROLLBACKcalls), soBEGINis not re-executed on an already-started transaction during replay andCOMMIT/ROLLBACKcannot leak into the next transaction's retry history. FailedBEGINcalls are still recorded so retries verify the same error.