Does begin_nested() automatically rollback/commit?

Question:

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?

Asked By: Kar

||

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

Answered By: davidism
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.