Skip to content
Snippets Groups Projects
Commit ad7d5158 authored by Ruud Beukema's avatar Ruud Beukema
Browse files

Task #10813: Improved reconnect to RADB mechanism in _executeQuery() such that...

Task #10813: Improved reconnect to RADB mechanism in _executeQuery() such that self.cursor.execute(query, qargs) is always called from within a try-statement
parent d897c75e
No related branches found
No related tags found
No related merge requests found
...@@ -67,27 +67,29 @@ class RADatabase: ...@@ -67,27 +67,29 @@ class RADatabase:
return line return line
def _executeQuery(self, query, qargs=None, fetch=_FETCH_NONE): def _executeQuery(self, query, qargs=None, fetch=_FETCH_NONE):
'''execute the query and reconnect upon OperationalError''' ''' Execute the query and reconnect upon OperationalError '''
try:
if self.log_queries: if self.log_queries:
logger.info('executing query: %s' % self._queryAsSingleLine(query, qargs)) logger.info('executing query: %s' % self._queryAsSingleLine(query, qargs))
self.cursor.execute(query, qargs) # Allow for 5 connection retries
except (psycopg2.OperationalError, AttributeError) as e: for i in range(5):
if isinstance(e, psycopg2.OperationalError): try:
logger.error(str(e)) self.cursor.execute(query, qargs)
for i in range(5): break
except (psycopg2.OperationalError, AttributeError) as e:
if isinstance(e, psycopg2.OperationalError):
logger.error(str(e))
logger.info("(re)trying to connect to radb") logger.info("(re)trying to connect to radb")
self._connect() self._connect()
if self.conn: if self.conn:
logger.info("connected to radb") logger.info("connected to radb")
self.cursor.execute(query, qargs)
break
time.sleep(i*i) time.sleep(i*i)
except (psycopg2.IntegrityError, psycopg2.ProgrammingError, psycopg2.InternalError, psycopg2.DataError)as e: except (psycopg2.IntegrityError, psycopg2.ProgrammingError, psycopg2.InternalError, psycopg2.DataError)as e:
logger.error("Rolling back query=\'%s\' due to error: \'%s\'" % (self._queryAsSingleLine(query, qargs), e)) logger.error("Rolling back query=\'%s\' due to error: \'%s\'" % (self._queryAsSingleLine(query, qargs), e))
self.rollback() self.rollback()
return [] return []
if fetch == _FETCH_ONE: if fetch == _FETCH_ONE:
return self.cursor.fetchone() return self.cursor.fetchone()
...@@ -463,9 +465,9 @@ class RADatabase: ...@@ -463,9 +465,9 @@ class RADatabase:
return predIdDict return predIdDict
def getTaskSuccessorIds(self, id=None): def getTaskSuccessorIds(self, id=None):
query = '''SELECT * from resource_allocation.task_predecessor tp;''' query = '''SELECT * from resource_allocation.task_predecessor tp'''
if id is not None : if id is not None:
query += ' WHERE id=%s' query += ' WHERE id=%s'
items = list(self._executeQuery(query, [id] if id is not None else None, fetch=_FETCH_ALL)) items = list(self._executeQuery(query, [id] if id is not None else None, fetch=_FETCH_ALL))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment