session_db: improve resiliency to database errors

Retry on OperationalError exception, which we receive on database restart.
Return cursor to pool when reconnecting.
pull/2531/head
Stéphane Bidoul 2023-01-21 13:27:21 +01:00
parent 061a51c354
commit 018b664940
1 changed files with 8 additions and 1 deletions

View File

@ -23,7 +23,7 @@ def with_cursor(func):
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
@ -49,6 +49,13 @@ class PGSessionStore(sessions.SessionStore):
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