From 018b6649409b17c4185be5b06efbe01449f8a334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Sat, 21 Jan 2023 13:27:21 +0100 Subject: [PATCH] session_db: improve resiliency to database errors Retry on OperationalError exception, which we receive on database restart. Return cursor to pool when reconnecting. --- session_db/pg_session_store.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/session_db/pg_session_store.py b/session_db/pg_session_store.py index 3feae5f2b..e5974232d 100644 --- a/session_db/pg_session_store.py +++ b/session_db/pg_session_store.py @@ -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