def create_session(self):
"""
Create a session context that communicates with the database.
Commits all changes to the database before closing the session, and if an exception is raised,
rollback the session.
"""
session = Session(self.connection)
logger.info("Create session {0} with {1}".format(
id(session), self._public_db_uri(str(self.engine.url))))
try:
yield session
session.commit()
logger.info("Commit transactions to database")
except Exception:
session.rollback()
logger.exception("Database transactions rolled back")
finally:
logger.info("Session {0} with {1} closed".format(
id(session), self._public_db_uri(str(self.engine.url))))
session.close()
评论列表
文章目录