@@ -156,7 +156,7 @@ async def iterate(
156
156
yield Record (row , result_columns , self ._dialect , column_maps )
157
157
158
158
def transaction (self ) -> "TransactionBackend" :
159
- raise NotImplementedError () # pragma: no cover
159
+ return PsycopgTransaction ( connection = self )
160
160
161
161
@property
162
162
def raw_connection (self ) -> typing .Any :
@@ -166,13 +166,36 @@ def raw_connection(self) -> typing.Any:
166
166
167
167
168
168
class PsycopgTransaction (TransactionBackend ):
169
+ _connecttion : PsycopgConnection
170
+ _transaction : typing .Optional [psycopg .AsyncTransaction ]
171
+
172
+ def __init__ (self , connection : PsycopgConnection ):
173
+ self ._connection = connection
174
+ self ._transaction : typing .Optional [psycopg .AsyncTransaction ] = None
175
+
169
176
async def start (
170
177
self , is_root : bool , extra_options : typing .Dict [typing .Any , typing .Any ]
171
178
) -> None :
172
- raise NotImplementedError () # pragma: no cover
179
+ if self ._connection ._connection is None :
180
+ raise RuntimeError ("Connection is not acquired" )
181
+
182
+ transaction = psycopg .AsyncTransaction (
183
+ self ._connection ._connection , ** extra_options
184
+ )
185
+ async with transaction ._conn .lock :
186
+ await transaction ._conn .wait (transaction ._enter_gen ())
187
+ self ._transaction = transaction
173
188
174
189
async def commit (self ) -> None :
175
- raise NotImplementedError () # pragma: no cover
190
+ if self ._transaction is None :
191
+ raise RuntimeError ("Transaction was not started" )
192
+
193
+ async with self ._transaction ._conn .lock :
194
+ await self ._transaction ._conn .wait (self ._transaction ._commit_gen ())
176
195
177
196
async def rollback (self ) -> None :
178
- raise NotImplementedError () # pragma: no cover
197
+ if self ._transaction is None :
198
+ raise RuntimeError ("Transaction was not started" )
199
+
200
+ async with self ._transaction ._conn .lock :
201
+ await self ._transaction ._conn .wait (self ._transaction ._rollback_gen (None ))
0 commit comments