Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does begin_nested() automatically rollback/commit?

When begin_nested is used as a context manager, e.g.

with db.session.begin_nested:
    # do something

If an IntegrityError is thrown, will db.session.rollbank() be called automatically? On the contrary, if no exception is thrown, will db.session.commit() be automatically called?

like image 426
Kar Avatar asked Jan 27 '14 10:01

Kar


1 Answers

If a transaction, such as one from begin_nested, is used as a context manager, the transaction is commited at exit, or rolled back if there was an error in the block or during commit.

Here is the relevant source: https://github.com/zzzeek/sqlalchemy/blob/81518ae2e2bc622f8cd47287a575ad4c0e43ead1/lib/sqlalchemy/orm/session.py#L558-L569

like image 147
davidism Avatar answered Oct 29 '22 11:10

davidism