def session_scope(): session = Session() try: yield session session.commit() except Exception: session.rollback() raise finally: session.close()
def create_session(): """Contextmanager that will create and teardown a session.""" session = Session() try: yield session session.commit() except Exception: session.rollback() raise finally: session.close()