- 欢迎来到 GINO 的文档
- 上手教程
- 进阶用法
- 原理说明
- 参考手册
- API 参考
- gino.bakery module
- gino.api module
- gino.declarative module
- gino.schema module
- gino.crud module
- gino.json_support module
- gino.engine module
- gino.loader module
- gino.dialects.asyncpg module
- gino.dialects.base module
- gino.transaction module
- gino.dialects package
- gino.dialects.aiomysql module
- gino.ext package
- gino.aiocontextvars module
- gino.exceptions module
- gino.strategies module
- 扩展
- 版本历史
- API 参考
gino.transaction module
- class
gino.transaction.
GinoTransaction
(conn, args, kwargs)¶ 基类:
object
Represents an underlying database transaction and its connection, offering methods to manage this transaction.
GinoTransaction
is supposed to be created by eithergino.engine.GinoConnection.transaction()
, orgino.engine.GinoEngine.transaction()
, orgino.api.Gino.transaction()
, shown as follows:async with db.transaction() as tx: ... async with engine.transaction() as tx: ... async with conn.transaction() as tx: ... tx = await conn.transaction() try: ... await tx.commit() except Exception: await tx.rollback() raise
When in use with asynchronous context manager,
GinoTransaction
will be in managed mode, while the last example withawait
will put theGinoTransaction
in manual mode where you have to call thecommit()
orrollback()
to manually close the transaction.In managed mode the transaction will be automatically committed or rolled back on exiting the
async with
block depending on whether there is an exception or not. Meanwhile, you can explicitly exit the transaction early byraise_commit()
orraise_rollback()
which will raise an internal exception managed by the asynchronous context manager and interpreted as a commit or rollback action. In a nested transaction situation, the two exit-early methods always close up the very transaction which the two methods are referenced upon - all children transactions are either committed or rolled back correspondingly, while no parent transaction was ever touched. For example:async with db.transaction() as tx1: async with db.transaction() as tx2: async with db.transaction() as tx3: tx2.raise_rollback() # Won't reach here # Won't reach here # Continues here with tx1, with both tx2 and tx3 rolled back. # For PostgreSQL, tx1 can still be committed successfully because # tx2 and tx3 are just SAVEPOINTs in transaction tx1
小技巧
The internal exception raised from
raise_commit()
andraise_rollback()
is a subclass ofBaseException
, so normaltry ... except Exception:
can't trap the commit or rollback.- async
commit
()¶ Only available in manual mode: manually commit this transaction.
- property
connection
¶ Accesses to the
GinoConnection
of this transaction. This is useful if when the transaction is started fromdb
orengine
where the connection is implicitly acquired for you together with the transaction.
raise_commit
()¶Only available in managed mode: skip rest of the code in this transaction and commit immediately by raising an internal exception, which will be caught and handled by the asynchronous context manager:
async with db.transaction() as tx: await user.update(age=64).apply() tx.raise_commit() await user.update(age=32).apply() # won't reach here assert user.age == 64 # no exception raised before
raise_rollback
()¶Only available in managed mode: skip rest of the code in this transaction and rollback immediately by raising an internal exception, which will be caught and handled by the asynchronous context manager:
assert user.age == 64 # assumption async with db.transaction() as tx: await user.update(age=32).apply() tx.raise_rollback() await user.update(age=128).apply() # won't reach here assert user.age == 64 # no exception raised before
- property
raw_transaction
¶ Accesses to the underlying transaction object, whose type depends on the dialect in use.
- async
rollback
()¶ Only available in manual mode: manually rollback this transaction.
- async
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论