@@ -42,14 +42,14 @@ class _IsolationSettings(NamedTuple):
4242 ydb .QuerySerializableReadWrite (), interactive = True
4343 ),
4444 IsolationLevel .ONLINE_READONLY : _IsolationSettings (
45- ydb .QueryOnlineReadOnly (), interactive = True
45+ ydb .QueryOnlineReadOnly (), interactive = False
4646 ),
4747 IsolationLevel .ONLINE_READONLY_INCONSISTENT : _IsolationSettings (
4848 ydb .QueryOnlineReadOnly ().with_allow_inconsistent_reads (),
49- interactive = True ,
49+ interactive = False ,
5050 ),
5151 IsolationLevel .STALE_READONLY : _IsolationSettings (
52- ydb .QueryStaleReadOnly (), interactive = True
52+ ydb .QueryStaleReadOnly (), interactive = False
5353 ),
5454 IsolationLevel .SNAPSHOT_READONLY : _IsolationSettings (
5555 ydb .QuerySnapshotReadOnly (), interactive = True
@@ -78,10 +78,11 @@ def __init__(
7878
7979 self .connection_kwargs : dict = kwargs
8080
81- self ._tx_mode : ydb .BaseQueryTxMode = ydb .QuerySerializableReadWrite ()
81+ self ._shared_session_pool : bool = False
82+
8283 self ._tx_context : TxContext | AsyncTxContext | None = None
84+ self ._tx_mode : ydb .BaseQueryTxMode = ydb .QuerySerializableReadWrite ()
8385 self .interactive_transaction : bool = False
84- self ._shared_session_pool : bool = False
8586
8687 if ydb_session_pool is not None :
8788 self ._shared_session_pool = True
@@ -99,11 +100,13 @@ def __init__(
99100 self ._session : ydb .QuerySession | ydb .aio .QuerySession | None = None
100101
101102 def set_isolation_level (self , isolation_level : IsolationLevel ) -> None :
102- ydb_isolation_settings = _ydb_isolation_settings_map [isolation_level ]
103103 if self ._tx_context and self ._tx_context .tx_id :
104104 raise InternalError (
105105 "Failed to set transaction mode: transaction is already began"
106106 )
107+
108+ ydb_isolation_settings = _ydb_isolation_settings_map [isolation_level ]
109+
107110 self ._tx_mode = ydb_isolation_settings .ydb_mode
108111 self .interactive_transaction = ydb_isolation_settings .interactive
109112
@@ -113,7 +116,7 @@ def get_isolation_level(self) -> str:
113116 return IsolationLevel .SERIALIZABLE
114117 return IsolationLevel .AUTOCOMMIT
115118 if self ._tx_mode .name == ydb .QueryOnlineReadOnly ().name :
116- if self ._tx_mode .settings . allow_inconsistent_reads :
119+ if self ._tx_mode .allow_inconsistent_reads :
117120 return IsolationLevel .ONLINE_READONLY_INCONSISTENT
118121 return IsolationLevel .ONLINE_READONLY
119122 if self ._tx_mode .name == ydb .QueryStaleReadOnly ().name :
@@ -123,6 +126,12 @@ def get_isolation_level(self) -> str:
123126 msg = f"{ self ._tx_mode .name } is not supported"
124127 raise NotSupportedError (msg )
125128
129+ def _maybe_init_tx (
130+ self , session : ydb .QuerySession | ydb .aio .QuerySession
131+ ) -> None :
132+ if self ._tx_context is None and self .interactive_transaction :
133+ self ._tx_context = session .transaction (self ._tx_mode )
134+
126135
127136class Connection (BaseConnection ):
128137 _driver_cls = ydb .Driver
@@ -154,15 +163,12 @@ def cursor(self) -> Cursor:
154163 if self ._session is None :
155164 raise RuntimeError ("Connection is not ready, use wait_ready." )
156165
157- if self .interactive_transaction :
158- self ._tx_context = self ._session .transaction (self ._tx_mode )
159- else :
160- self ._tx_context = None
166+ self ._maybe_init_tx (self ._session )
161167
162168 self ._current_cursor = self ._cursor_cls (
163169 session = self ._session ,
170+ tx_mode = self ._tx_mode ,
164171 tx_context = self ._tx_context ,
165- autocommit = (not self .interactive_transaction ),
166172 )
167173 return self ._current_cursor
168174
@@ -181,16 +187,19 @@ def wait_ready(self, timeout: int = 10) -> None:
181187
182188 self ._session = self ._session_pool .acquire ()
183189
190+ @handle_ydb_errors
184191 def commit (self ) -> None :
185192 if self ._tx_context and self ._tx_context .tx_id :
186193 self ._tx_context .commit ()
187194 self ._tx_context = None
188195
196+ @handle_ydb_errors
189197 def rollback (self ) -> None :
190198 if self ._tx_context and self ._tx_context .tx_id :
191199 self ._tx_context .rollback ()
192200 self ._tx_context = None
193201
202+ @handle_ydb_errors
194203 def close (self ) -> None :
195204 self .rollback ()
196205
@@ -281,15 +290,12 @@ def cursor(self) -> AsyncCursor:
281290 if self ._session is None :
282291 raise RuntimeError ("Connection is not ready, use wait_ready." )
283292
284- if self .interactive_transaction :
285- self ._tx_context = self ._session .transaction (self ._tx_mode )
286- else :
287- self ._tx_context = None
293+ self ._maybe_init_tx (self ._session )
288294
289295 self ._current_cursor = self ._cursor_cls (
290296 session = self ._session ,
297+ tx_mode = self ._tx_mode ,
291298 tx_context = self ._tx_context ,
292- autocommit = (not self .interactive_transaction ),
293299 )
294300 return self ._current_cursor
295301
@@ -308,16 +314,19 @@ async def wait_ready(self, timeout: int = 10) -> None:
308314
309315 self ._session = await self ._session_pool .acquire ()
310316
317+ @handle_ydb_errors
311318 async def commit (self ) -> None :
312319 if self ._tx_context and self ._tx_context .tx_id :
313320 await self ._tx_context .commit ()
314321 self ._tx_context = None
315322
323+ @handle_ydb_errors
316324 async def rollback (self ) -> None :
317325 if self ._tx_context and self ._tx_context .tx_id :
318326 await self ._tx_context .rollback ()
319327 self ._tx_context = None
320328
329+ @handle_ydb_errors
321330 async def close (self ) -> None :
322331 await self .rollback ()
323332
0 commit comments