def cursor(self):
"""
Returns a database cursor, automatically re-opening the database
connection if necessary.
"""
try:
try:
cursor = self.db.cursor()
cursor.execute('SELECT 1')
except psycopg2.InternalError as err:
if err.pgcode == IN_FAILED_SQL_TRANSACTION:
LOGGER.critical("Rolling back aborted transaction...")
self.db.rollback()
else:
LOGGER.critical("PostgreSQL reported an internal error "
"I don't know how to handle: %s "
"(code=%s)", pg_err_lookup(err.pgcode),
err.pgcode)
raise
except Exception as err:
if self.db is not None:
LOGGER.critical("Could not get cursor. Trying to reconnect...",
exc_info=True)
self.close()
self.connect()
cursor = self.db.cursor()
return cursor
评论列表
文章目录