@@ -30,26 +30,24 @@ class IsolationLevel(str, Enum):
30
30
31
31
32
32
class _IsolationSettings (NamedTuple ):
33
- ydb_mode : ydb .BaseQueryTxMode
33
+ ydb_mode : ydb .BaseQueryTxMode | None
34
34
interactive : bool
35
35
36
36
37
37
_ydb_isolation_settings_map = {
38
- IsolationLevel .AUTOCOMMIT : _IsolationSettings (
39
- ydb .QuerySerializableReadWrite (), interactive = False
40
- ),
38
+ IsolationLevel .AUTOCOMMIT : _IsolationSettings (None , interactive = False ),
41
39
IsolationLevel .SERIALIZABLE : _IsolationSettings (
42
40
ydb .QuerySerializableReadWrite (), interactive = True
43
41
),
44
42
IsolationLevel .ONLINE_READONLY : _IsolationSettings (
45
- ydb .QueryOnlineReadOnly (), interactive = True
43
+ ydb .QueryOnlineReadOnly (), interactive = False
46
44
),
47
45
IsolationLevel .ONLINE_READONLY_INCONSISTENT : _IsolationSettings (
48
46
ydb .QueryOnlineReadOnly ().with_allow_inconsistent_reads (),
49
- interactive = True ,
47
+ interactive = False ,
50
48
),
51
49
IsolationLevel .STALE_READONLY : _IsolationSettings (
52
- ydb .QueryStaleReadOnly (), interactive = True
50
+ ydb .QueryStaleReadOnly (), interactive = False
53
51
),
54
52
IsolationLevel .SNAPSHOT_READONLY : _IsolationSettings (
55
53
ydb .QuerySnapshotReadOnly (), interactive = True
@@ -78,10 +76,11 @@ def __init__(
78
76
79
77
self .connection_kwargs : dict = kwargs
80
78
81
- self ._tx_mode : ydb .BaseQueryTxMode = ydb .QuerySerializableReadWrite ()
79
+ self ._shared_session_pool : bool = False
80
+
82
81
self ._tx_context : TxContext | AsyncTxContext | None = None
82
+ self ._tx_mode : ydb .BaseQueryTxMode | None = None
83
83
self .interactive_transaction : bool = False
84
- self ._shared_session_pool : bool = False
85
84
86
85
if ydb_session_pool is not None :
87
86
self ._shared_session_pool = True
@@ -99,21 +98,24 @@ def __init__(
99
98
self ._session : ydb .QuerySession | ydb .aio .QuerySession | None = None
100
99
101
100
def set_isolation_level (self , isolation_level : IsolationLevel ) -> None :
102
- ydb_isolation_settings = _ydb_isolation_settings_map [isolation_level ]
103
101
if self ._tx_context and self ._tx_context .tx_id :
104
102
raise InternalError (
105
103
"Failed to set transaction mode: transaction is already began"
106
104
)
105
+
106
+ ydb_isolation_settings = _ydb_isolation_settings_map [isolation_level ]
107
+
108
+ self ._tx_context = None
107
109
self ._tx_mode = ydb_isolation_settings .ydb_mode
108
110
self .interactive_transaction = ydb_isolation_settings .interactive
109
111
110
112
def get_isolation_level (self ) -> str :
111
- if self ._tx_mode .name == ydb .QuerySerializableReadWrite ().name :
112
- if self .interactive_transaction :
113
- return IsolationLevel .SERIALIZABLE
113
+ if self ._tx_mode is None :
114
114
return IsolationLevel .AUTOCOMMIT
115
+ if self ._tx_mode .name == ydb .QuerySerializableReadWrite ().name :
116
+ return IsolationLevel .SERIALIZABLE
115
117
if self ._tx_mode .name == ydb .QueryOnlineReadOnly ().name :
116
- if self ._tx_mode .settings . allow_inconsistent_reads :
118
+ if self ._tx_mode .allow_inconsistent_reads :
117
119
return IsolationLevel .ONLINE_READONLY_INCONSISTENT
118
120
return IsolationLevel .ONLINE_READONLY
119
121
if self ._tx_mode .name == ydb .QueryStaleReadOnly ().name :
@@ -123,6 +125,12 @@ def get_isolation_level(self) -> str:
123
125
msg = f"{ self ._tx_mode .name } is not supported"
124
126
raise NotSupportedError (msg )
125
127
128
+ def _maybe_init_tx (
129
+ self , session : ydb .QuerySession | ydb .aio .QuerySession
130
+ ) -> None :
131
+ if self ._tx_context is None and self ._tx_mode is not None :
132
+ self ._tx_context = session .transaction (self ._tx_mode )
133
+
126
134
127
135
class Connection (BaseConnection ):
128
136
_driver_cls = ydb .Driver
@@ -154,10 +162,7 @@ def cursor(self) -> Cursor:
154
162
if self ._session is None :
155
163
raise RuntimeError ("Connection is not ready, use wait_ready." )
156
164
157
- if self .interactive_transaction :
158
- self ._tx_context = self ._session .transaction (self ._tx_mode )
159
- else :
160
- self ._tx_context = None
165
+ self ._maybe_init_tx (self ._session )
161
166
162
167
self ._current_cursor = self ._cursor_cls (
163
168
session = self ._session ,
@@ -281,10 +286,7 @@ def cursor(self) -> AsyncCursor:
281
286
if self ._session is None :
282
287
raise RuntimeError ("Connection is not ready, use wait_ready." )
283
288
284
- if self .interactive_transaction :
285
- self ._tx_context = self ._session .transaction (self ._tx_mode )
286
- else :
287
- self ._tx_context = None
289
+ self ._maybe_init_tx (self ._session )
288
290
289
291
self ._current_cursor = self ._cursor_cls (
290
292
session = self ._session ,
0 commit comments