SAVEPOINT transactions

The problem

In a function, you want to perform some operations on a database using sqlalchemy's session. If one of the operations fails, you want the session to go back to its state when the function was called.

The solution

Use a SAVEPOINT transaction:

from nagare import database
def my_func():
        with database.session.begin(nested=True):
            # let the job be done in a SAVEPOINT transaction
            # if an exception is raised in this block, session's state is restored
        return True
        return False


No comments.