I have created a context manager to get a transactional session, following the instructions here:
@contextmanager
def session_scope():
"""Provide a transactional scope around a series of operations."""
session = Session()
try:
yield session
session.commit()
except:
session.rollback()
raise
finally:
session.close()
And it's working great. Now I'd like to do something like that but for sessions that I know are only used to fetch values from the database.
Instead of session.commit()
, what would be the best thing to do with the session? Just .rollback()
and that's it?