Skip to content

Read tx context from connection on cursor execute - #44

Merged
vgvoleg merged 1 commit into
mainfrom
fix/cursor-reads-tx-context
Aug 27, 2026
Merged

Read tx context from connection on cursor execute#44
vgvoleg merged 1 commit into
mainfrom
fix/cursor-reads-tx-context

Conversation

@vgvoleg

@vgvoleg vgvoleg commented Aug 6, 2026

Copy link
Copy Markdown
Member

Fixes #34

Cursors snapshotted connection._tx_context at creation time, so a cursor created before begin() silently ran outside the transaction, and a cursor reused after commit()/rollback() kept executing against a dead transaction.

Cursor._tx_context / AsyncCursor._tx_context are now read-only properties delegating to the connection, matching how psycopg2/asyncpg treat the transaction as connection state. The tx_context constructor argument is dropped — Connection.cursor() no longer passes it.

Tests cover both scenarios from the issue: insert through a pre-begin() cursor is undone by rollback(), and a cursor stays usable after commit().

@vgvoleg
vgvoleg requested review from LuckySting and a lite review from Copilot August 6, 2026 11:56

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_context from Cursor / AsyncCursor constructors and stop passing it from Connection.cursor().
  • Implement Cursor._tx_context and AsyncCursor._tx_context as read-only properties that delegate to connection._tx_context.
  • Add integration tests covering: (1) cursor created before begin() participates in the later transaction, and (2) cursor remains usable after commit().

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.

@vgvoleg
vgvoleg force-pushed the fix/cursor-reads-tx-context branch from b7351f4 to 6e445ee Compare August 27, 2026 11:41
@vgvoleg
vgvoleg merged commit 983a4d4 into main Aug 27, 2026
6 checks passed
@vgvoleg
vgvoleg deleted the fix/cursor-reads-tx-context branch August 27, 2026 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cursor snapshots tx_context at creation time, causing surprising behavior across begin/commit

3 participants