Skip to content
Snippets Groups Projects
Commit a317dbbc authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

Task #10902: log db notices also before rollback after db exception

parent c37bfce4
No related branches found
No related tags found
No related merge requests found
......@@ -68,6 +68,13 @@ class RADatabase:
line = line % tuple(qargs)
return line
def _log_db_notices(self):
'''print all most recent notices raised by the db as log info string, and reset the most recent notices list'''
if self.conn.notices:
for notice in self.conn.notices:
logger.info('database log message %s', notice.strip())
del self.conn.notices[:]
def _executeQuery(self, query, qargs=None, fetch=_FETCH_NONE):
''' Execute the query and reconnect upon OperationalError '''
......@@ -78,6 +85,7 @@ class RADatabase:
for i in range(5):
try:
self.cursor.execute(query, qargs)
self._log_db_notices()
break
except (psycopg2.OperationalError, AttributeError) as e:
if isinstance(e, psycopg2.OperationalError):
......@@ -89,15 +97,11 @@ class RADatabase:
logger.info("connected to radb")
time.sleep(i*i)
except (psycopg2.IntegrityError, psycopg2.ProgrammingError, psycopg2.InternalError, psycopg2.DataError)as e:
self._log_db_notices()
logger.error("Rolling back query=\'%s\' due to error: \'%s\'" % (self._queryAsSingleLine(query, qargs), e))
self.rollback()
return []
if self.conn.notices:
for notice in self.conn.notices:
logger.info('database log message %s', notice.strip())
del self.conn.notices[:]
if fetch == _FETCH_ONE:
return self.cursor.fetchone()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment