fix(sessions): use read-only transactions for get/list operations#4810
fix(sessions): use read-only transactions for get/list operations#4810giulio-leone wants to merge 1 commit intogoogle:mainfrom
Conversation
DatabaseSessionService.get_session() and list_sessions() are pure-read operations but currently open regular read-write transactions via _rollback_on_exception_session(). On Cloud Spanner this causes RetryAborted errors when a concurrent write commits during the read, because Spanner's OCC layer sees the read-write transaction as conflicting. Add _readonly_session() context manager that: - Marks the connection as read-only (postgresql_readonly=True) which also benefits Cloud Spanner's sqlalchemy-spanner dialect - Never commits, avoiding unnecessary write-path overhead - Still rolls back on exception to release the connection cleanly Switch get_session() and list_sessions() to use _readonly_session(). Write operations (create_session, delete_session, append_event) continue to use _rollback_on_exception_session() with explicit commits. Fixes google#4771 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
Hi @giulio-leone , Thank you for your contribution! It appears you haven't yet signed the Contributor License Agreement (CLA). Please visit https://cla.developers.google.com/ to complete the signing process. Once the CLA is signed, we'll be able to proceed with the review of your PR. Thank you! |
Summary
Fixes #4771
DatabaseSessionService.get_session()andlist_sessions()are pure-read operations but currently open regular read-write transactions via_rollback_on_exception_session(). On Cloud Spanner this causesRetryAbortederrors when a concurrent write commits during the read, because Spanner's OCC (Optimistic Concurrency Control) layer sees the read-write transaction as conflicting.Root Cause
Spanner uses OCC for read-write transactions. Even a
SELECT-only transaction opened in read-write mode will be aborted if a concurrent write touches the same data. Since reads never mutate data, they should use read-only transactions which are not subject to OCC aborts.Fix
Added a
_readonly_session()context manager that:execution_options(postgresql_readonly=True)sqlalchemy-spannerdialect to open read-only transactionsdatabase_session_factoryand connection poolSwitched
get_session()andlist_sessions()to use_readonly_session().Write operations (
create_session,delete_session,append_event) continue to use_rollback_on_exception_session()with explicit commits — unchanged.Testing
All 128 session tests pass. Full suite: 4725 passed, 0 failures, 0 regressions.