These, and only these, functions should help you with treating your own code in the context of one database transaction. Which makes our lives easier.



Central place to handle transactions finally. So - usually your view code should not have to deal with rolling back. Our policy is that we don’t auto-commit (we used to do that here). Some more reading is e.g. here https://github.com/pallets/flask-sqlalchemy/issues/216

Register this on your app via the teardown_request setup method. We roll back the session if there was any error (which only has an effect if the session has not yet been committed).

Flask-SQLAlchemy is closing the scoped sessions automatically.


Decorator for handling any function which contains SQLAlchemy commands as one database transaction (ACID). Calls db operation function and when it is done, commits the db session. Rolls back the session if anything goes wrong. If useful, the first argument can be the db (SQLAlchemy) object and the rest of the args are sent through to the function. If this happened, the session is closed at the end.


exception flexmeasures.data.transactional.PartialTaskCompletionException

By raising this Exception in a task, no rollback will happen even if not everything was successful and the data which was generated will get committed. The task status will still be False, so the non-successful parts can be inspected.