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 0 commit intogoogle:mainfrom
Conversation
|
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! |
93e0170 to
748e95a
Compare
|
@rohityan Thanks for the heads-up! I'll get the Google CLA signed. Will follow up once it's done. |
|
I have signed the Google CLA. Could you please re-run the CLA check? Thank you! |
9c25afd to
fc60159
Compare
|
Hi @rohityan — the CLA is now signed and passing ✅ (it was a |
|
/gemini review |
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
| """ | ||
| async with self.database_session_factory() as sql_session: | ||
| conn = await sql_session.connection() | ||
| await conn.execution_options(postgresql_readonly=True) |
There was a problem hiding this comment.
In SQLAlchemy, conn.execution_options() is synchronous. Will raise a TypeError.
You should use execution_options={"read_only": True} for spanner
0646e39 to
4b677e7
Compare
|
This branch has now been rebased to current I verified the absorbed fix locally from that refreshed head:
So the original contributor-side fix is already present upstream and this PR is effectively superseded/absorbed. |
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.