What are drawbacks of automatic rollbacks in case of exceptions?

Viewed 194

When you write a flask+sqlalchemy app and make a DB query + Exception handling like this:

def add_user(user: User) -> bool:  # was the user added?
    errors = []
    try:
        db.session.add(user)
        db.session.commit()
    except Exception:  # e.g. Integrity error
        return False

It can later show PendingRollbackError. A solution I've used in a couple of projects is proposed here:

@app.teardown_request
def teardown_request(exception):
    if exception:
        db.session.rollback()
    db.session.remove()

What are the disadvantages of this solution? If it doesn't have any, I wonder why this is not done by default.

0 Answers
Related