Skip to content Skip to sidebar Skip to footer

Does Closing A Sqlalchemy Orm Session Roll Back Uncommitted Changes?

For example, is there a difference between the following two? session = Session() # Session is a session maker try: # do some work session.commit() except: session.roll

Solution 1:

Closing a session will implicitly roll back current transactional state:

The close() method issues a expunge_all(), and releases any transactional/connection resources. When connections are returned to the connection pool, transactional state is rolled back as well.

But I'd argue that the first form is still better, since explicit is better than implicit. The author of SQLAlchemy also seems to reflect this sentiment.

Post a Comment for "Does Closing A Sqlalchemy Orm Session Roll Back Uncommitted Changes?"