Peewee – Atomic Transactions
”;
Peewee’s database class has atomic() method that creates a context manager. It starts a new transaction. Inside the context block, it is possible to commit or rollback the transaction depending upon whether it has been successfully done or it encountered exception.
with db.atomic() as transaction: try: User.create(name=''Amar'', age=20) transaction.commit() except DatabaseError: transaction.rollback()
The atomic() can also be used as decorator.
@db.atomic() def create_user(nm,n): return User.create(name=nm, age=n) create_user(''Amar'', 20)
More than one atomic transaction blocks can also be nested.
with db.atomic() as txn1: User.create(''name''=''Amar'', age=20) with db.atomic() as txn2: User.get(name=''Amar'')
Advertisements
”;