Skip to content

Commit

Permalink
session_db: improve resiliency to database errors
Browse files Browse the repository at this point in the history
Retry on OperationalError exception, which we receive on database restart.
Return cursor to pool when reconnecting.
  • Loading branch information
sbidoul committed Jan 23, 2023
1 parent 061a51c commit 018b664
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion session_db/pg_session_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def wrapper(self, *args, **kwargs):
tries += 1
try:
return func(self, *args, **kwargs)
except psycopg2.InterfaceError as e:
except (psycopg2.InterfaceError, psycopg2.OperationalError) as e:
_logger.info("Session in DB connection Retry %s/5" % tries)
if tries > 4:
raise e
Expand All @@ -49,6 +49,13 @@ def __del__(self):

def _open_connection(self):
cnx = odoo.sql_db.db_connect(self._uri, allow_uri=True)
try:
# return cursor to the pool
if self._cr is not None:
self._cr.close()
self._cr = None
except Exception: # pylint: disable=except-pass
pass
self._cr = cnx.cursor()
self._cr._cnx.autocommit = True

Expand Down

0 comments on commit 018b664

Please sign in to comment.