Read tx context from connection on cursor execute - #44
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes transaction-state drift between Connection and Cursor by making cursors read the active transaction context from the connection at execution time (instead of snapshotting it when the cursor is created). This aligns the driver behavior with common DB-API expectations and addresses the incorrect behavior described in issue #34.
Changes:
- Remove
tx_contextfromCursor/AsyncCursorconstructors and stop passing it fromConnection.cursor(). - Implement
Cursor._tx_contextandAsyncCursor._tx_contextas read-only properties that delegate toconnection._tx_context. - Add integration tests covering: (1) cursor created before
begin()participates in the later transaction, and (2) cursor remains usable aftercommit().
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| ydb_dbapi/cursors.py | Make cursor transaction context a connection-backed property to avoid snapshotting stale/None tx state. |
| ydb_dbapi/connections.py | Stop passing tx_context into cursor constructors; rely on connection state instead. |
| tests/test_cursors.py | Update fake connection stubs to include _tx_context for the new cursor delegation behavior. |
| tests/test_connections.py | Add regression tests for cursor behavior across begin()/commit()/rollback() ordering. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
LuckySting
approved these changes
Aug 27, 2026
vgvoleg
force-pushed
the
fix/cursor-reads-tx-context
branch
from
August 27, 2026 11:41
b7351f4 to
6e445ee
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #34
Cursors snapshotted
connection._tx_contextat creation time, so a cursor created beforebegin()silently ran outside the transaction, and a cursor reused aftercommit()/rollback()kept executing against a dead transaction.Cursor._tx_context/AsyncCursor._tx_contextare now read-only properties delegating to the connection, matching howpsycopg2/asyncpgtreat the transaction as connection state. Thetx_contextconstructor argument is dropped —Connection.cursor()no longer passes it.Tests cover both scenarios from the issue: insert through a pre-
begin()cursor is undone byrollback(), and a cursor stays usable aftercommit().