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 either gino.engine.GinoConnection.transaction(), or gino.engine.GinoEngine.transaction(), or gino.api.Gino.transaction(), shown as follows:

  1. async with db.transaction() as tx:
  2. ...
  3. async with engine.transaction() as tx:
  4. ...
  5. async with conn.transaction() as tx:
  6. ...
  7. tx = await conn.transaction()
  8. try:
  9. ...
  10. await tx.commit()
  11. except Exception:
  12. await tx.rollback()
  13. raise

When in use with asynchronous context manager, GinoTransaction will be in managed mode, while the last example with await will put the GinoTransaction in manual mode where you have to call the commit() or rollback() 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 by raise_commit() or raise_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:

  1. async with db.transaction() as tx1:
  2. async with db.transaction() as tx2:
  3. async with db.transaction() as tx3:
  4. tx2.raise_rollback()
  5. # Won't reach here
  6. # Won't reach here
  7. # Continues here with tx1, with both tx2 and tx3 rolled back.
  8. # For PostgreSQL, tx1 can still be committed successfully because
  9. # tx2 and tx3 are just SAVEPOINTs in transaction tx1

小技巧

The internal exception raised from raise_commit() and raise_rollback() is a subclass of BaseException, so normal try ... 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 from db or engine 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:

    1. async with db.transaction() as tx:
    2. await user.update(age=64).apply()
    3. tx.raise_commit()
    4. await user.update(age=32).apply() # won't reach here
    5. 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:

    1. assert user.age == 64 # assumption
    2. async with db.transaction() as tx:
    3. await user.update(age=32).apply()
    4. tx.raise_rollback()
    5. await user.update(age=128).apply() # won't reach here
    6. 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.