Skip to content
Advertisement

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.rollback () be called automatically? On the contrary, if no exception is thrown, will db.session.commit() be automatically called?

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement