Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: release sessions without transaction id. #68

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions ydb_sqlalchemy/dbapi/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __init__(
self.interactive_transaction: bool = False # AUTOCOMMIT
self.tx_mode: ydb.AbstractTransactionModeBuilder = ydb.SerializableReadWrite()
self.tx_context: Optional[ydb.TxContext] = None
self.session: Optional[ydb.Session] = None
self.use_scan_query: bool = False
self.request_settings: ydb.BaseRequestSettings = ydb.BaseRequestSettings()

Expand Down Expand Up @@ -139,22 +140,29 @@ def get_ydb_request_settings(self) -> ydb.BaseRequestSettings:

def begin(self):
self.tx_context = None
if self.session:
self._maybe_await(self.session_pool.release, self.session)
self.session = None
if self.interactive_transaction and not self.use_scan_query:
session = self._maybe_await(self.session_pool.acquire)
self.tx_context = session.transaction(self.tx_mode)
self.session = self._maybe_await(self.session_pool.acquire)
self.tx_context = self.session.transaction(self.tx_mode)
self._maybe_await(self.tx_context.begin)

def commit(self):
if self.tx_context and self.tx_context.tx_id:
self._maybe_await(self.tx_context.commit)
self._maybe_await(self.session_pool.release, self.tx_context.session)
self.tx_context = None
if self.session:
self._maybe_await(self.session_pool.release, self.session)
self.session = None

def rollback(self):
if self.tx_context and self.tx_context.tx_id:
self._maybe_await(self.tx_context.rollback)
self._maybe_await(self.session_pool.release, self.tx_context.session)
self.tx_context = None
if self.session:
self._maybe_await(self.session_pool.release, self.session)
self.session = None

def close(self):
self.rollback()
Expand Down
Loading