deal
Database transactions are supported due to the encoding/database used to issue asynchronous queries.
basic usage
To use transactions, use database.transaction as the asynchronous context manager:
1 2 3 4 5 | |
!!!Note that it must be the same database used in the ormar_config object of the model.
To avoid passing database instances in code, you can extract instances from each model. The database provided during ormar.Model declaration is available through ormar_config.database and is accessible from classes and instances.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
You can also use.transaction() as a function decorator for any asynchronous function:
1 2 3 | |
Transaction blocks are managed as task local state. Nested transactions are fully supported and implemented using database savepoints.
Manual commit/rollback
For lower-level trading APIs, you can trigger it manually
1 2 3 4 5 6 7 8 | |
test
Transactions are also useful during testing when you can apply forced rollbacks and don't have to clean up data after each test.
1 2 3 4 5 6 | |