18
18
19
19
import asyncio
20
20
import typing as t
21
- from functools import wraps
22
21
23
22
from ..._async_compat .util import AsyncUtil
24
23
from ..._work import Query
@@ -57,7 +56,7 @@ def __init__(self, connection, fetch_size, on_closed, on_error,
57
56
self ._on_cancel = on_cancel
58
57
super ().__init__ ()
59
58
60
- async def _enter (self ):
59
+ async def _enter (self ) -> te . Self :
61
60
return self
62
61
63
62
@AsyncNonConcurrentMethodChecker .non_concurrent_method
@@ -113,7 +112,7 @@ async def run(
113
112
parameters : t .Optional [t .Dict [str , t .Any ]] = None ,
114
113
** kwparameters : t .Any
115
114
) -> AsyncResult :
116
- """ Run a Cypher query within the context of this transaction.
115
+ """Run a Cypher query within the context of this transaction.
117
116
118
117
Cypher is typically expressed as a query template plus a
119
118
set of named parameters. In Python, parameters may be expressed
@@ -172,10 +171,6 @@ async def run(
172
171
173
172
@AsyncNonConcurrentMethodChecker .non_concurrent_method
174
173
async def _commit (self ):
175
- """Mark this transaction as successful and close in order to trigger a COMMIT.
176
-
177
- :raise TransactionError: if the transaction is already closed
178
- """
179
174
if self ._closed_flag :
180
175
raise TransactionError (self , "Transaction closed" )
181
176
if self ._last_error :
@@ -202,10 +197,6 @@ async def _commit(self):
202
197
203
198
@AsyncNonConcurrentMethodChecker .non_concurrent_method
204
199
async def _rollback (self ):
205
- """Mark this transaction as unsuccessful and close in order to trigger a ROLLBACK.
206
-
207
- :raise TransactionError: if the transaction is already closed
208
- """
209
200
if self ._closed_flag :
210
201
raise TransactionError (self , "Transaction closed" )
211
202
@@ -228,88 +219,98 @@ async def _rollback(self):
228
219
229
220
@AsyncNonConcurrentMethodChecker .non_concurrent_method
230
221
async def _close (self ):
231
- """Close this transaction, triggering a ROLLBACK if not closed.
232
- """
233
222
if self ._closed_flag :
234
223
return
235
224
await self ._rollback ()
236
225
237
226
if AsyncUtil .is_async_code :
238
227
def _cancel (self ) -> None :
239
- """Cancel this transaction.
240
-
241
- If the transaction is already closed, this method does nothing.
242
- Else, it will close the connection without ROLLBACK or COMMIT in
243
- a non-blocking manner.
244
-
245
- The primary purpose of this function is to handle
246
- :class:`asyncio.CancelledError`.
247
-
248
- ::
249
-
250
- tx = await session.begin_transaction()
251
- try:
252
- ... # do some work
253
- except asyncio.CancelledError:
254
- tx.cancel()
255
- raise
256
-
257
- """
258
228
if self ._closed_flag :
259
229
return
260
230
try :
261
231
self ._on_cancel ()
262
232
finally :
263
233
self ._closed_flag = True
264
234
265
- def _closed (self ):
266
- """Indicate whether the transaction has been closed or cancelled.
267
-
268
- :returns:
269
- :data:`True` if closed or cancelled, :data:`False` otherwise.
270
- :rtype: bool
271
- """
235
+ def _closed (self ) -> bool :
272
236
return self ._closed_flag
273
237
274
238
275
239
class AsyncTransaction (AsyncTransactionBase ):
276
- """ Container for multiple Cypher queries to be executed within a single
240
+ """Fully user-managed transaction.
241
+
242
+ Container for multiple Cypher queries to be executed within a single
277
243
context. :class:`AsyncTransaction` objects can be used as a context
278
- managers (:py:const:`async with` block) where the transaction is committed
279
- or rolled back on based on whether an exception is raised::
244
+ manager (:py:const:`async with` block) where the transaction is committed
245
+ or rolled back based on whether an exception is raised::
280
246
281
247
async with await session.begin_transaction() as tx:
282
248
...
283
249
284
250
"""
285
251
286
- @wraps (AsyncTransactionBase ._enter )
287
252
async def __aenter__ (self ) -> AsyncTransaction :
288
253
return await self ._enter ()
289
254
290
- @wraps (AsyncTransactionBase ._exit )
291
- async def __aexit__ (self , exception_type , exception_value , traceback ):
255
+ async def __aexit__ (
256
+ self , exception_type , exception_value , traceback
257
+ ) -> None :
292
258
await self ._exit (exception_type , exception_value , traceback )
293
259
294
- @wraps (AsyncTransactionBase ._commit )
295
260
async def commit (self ) -> None :
261
+ """Commit the transaction and close it.
262
+
263
+ Marks this transaction as successful and closes in order to trigger a
264
+ COMMIT.
265
+
266
+ :raise TransactionError: if the transaction is already closed
267
+ """
296
268
return await self ._commit ()
297
269
298
- @wraps (AsyncTransactionBase ._rollback )
299
270
async def rollback (self ) -> None :
271
+ """Rollback the transaction and close it.
272
+
273
+ Marks the transaction as unsuccessful and closes in order to trigger
274
+ a ROLLBACK.
275
+
276
+ :raise TransactionError: if the transaction is already closed
277
+ """
300
278
return await self ._rollback ()
301
279
302
- @wraps (AsyncTransactionBase ._close )
303
280
async def close (self ) -> None :
281
+ """Close this transaction, triggering a ROLLBACK if not closed."""
304
282
return await self ._close ()
305
283
306
- @wraps (AsyncTransactionBase ._closed )
307
284
def closed (self ) -> bool :
285
+ """Indicate whether the transaction has been closed or cancelled.
286
+
287
+ :returns:
288
+ :data:`True` if closed or cancelled, :data:`False` otherwise.
289
+ :rtype: bool
290
+ """
308
291
return self ._closed ()
309
292
310
293
if AsyncUtil .is_async_code :
311
- @wraps (AsyncTransactionBase ._cancel )
312
294
def cancel (self ) -> None :
295
+ """Cancel this transaction.
296
+
297
+ If the transaction is already closed, this method does nothing.
298
+ Else, it will close the connection without ROLLBACK or COMMIT in
299
+ a non-blocking manner.
300
+
301
+ The primary purpose of this function is to handle
302
+ :class:`asyncio.CancelledError`.
303
+
304
+ ::
305
+
306
+ tx = await session.begin_transaction()
307
+ try:
308
+ ... # do some work
309
+ except asyncio.CancelledError:
310
+ tx.cancel()
311
+ raise
312
+
313
+ """
313
314
return self ._cancel ()
314
315
315
316
0 commit comments