diff --git a/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb.py b/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb.py index 93c8690bc8b289b5acbd312181186ee325c16ed1..4e0738129c5981de62915d93f485d16135e47648 100644 --- a/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb.py +++ b/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb.py @@ -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()