def execute(self, statement, values=None, commit=1):
"""
Runs a synchronized database query, ignoring any result rows.
Automatically re-opens a troubled connection, and handles errors.
"""
cursor = None
try:
cursor = self.cursor()
cursor.execute(statement, values)
LOGGER.debug("Executed: %s", cursor.query)
if commit:
try:
self.db.commit()
except Exception:
LOGGER.critical("Failed to commit")
except psycopg2.IntegrityError:
LOGGER.critical("Database integrity error, throwing away update",
exc_info=True)
LOGGER.debug("Tried to execute: %s", cursor.query)
if commit:
self.db.rollback()
except Exception:
LOGGER.critical("Could not execute statement: %s",
cursor.query if cursor else statement,
exc_info=True)
if commit:
self.db.rollback()
raise DbError()
评论列表
文章目录