From 2344802039fe9de267c02a1de60a5533b14b3527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Fri, 24 Mar 2023 09:38:44 +0100 Subject: [PATCH] session_db: refactor retry handling --- session_db/pg_session_store.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/session_db/pg_session_store.py b/session_db/pg_session_store.py index 5dc02fe0d..cf2dd607d 100644 --- a/session_db/pg_session_store.py +++ b/session_db/pg_session_store.py @@ -47,11 +47,14 @@ def with_cursor(func): try: self._ensure_connection() return func(self, *args, **kwargs) - except (psycopg2.InterfaceError, psycopg2.OperationalError) as e: - _logger.info("Session in DB connection Retry %s/5" % tries) + except (psycopg2.InterfaceError, psycopg2.OperationalError): + self._close_connection() if tries > 4: - raise e - self._open_connection() + _logger.warning( + "session_db operation try %s/5 failed, aborting", tries + ) + raise + _logger.info("session_db operation try %s/5 failed, retrying", tries) return wrapper @@ -65,8 +68,7 @@ class PGSessionStore(sessions.SessionStore): self._setup_db() def __del__(self): - if self._cr is not None: - self._cr.close() + self._close_connection() @with_lock def _ensure_connection(self): @@ -75,16 +77,20 @@ class PGSessionStore(sessions.SessionStore): @with_lock def _open_connection(self): - # return cursor to the pool + self._close_connection() + cnx = odoo.sql_db.db_connect(self._uri, allow_uri=True) + self._cr = cnx.cursor() + self._cr._cnx.autocommit = True + + @with_lock + def _close_connection(self): + """Return cursor to the pool.""" if self._cr is not None: try: self._cr.close() except Exception: # pylint: disable=except-pass pass self._cr = None - cnx = odoo.sql_db.db_connect(self._uri, allow_uri=True) - self._cr = cnx.cursor() - self._cr._cnx.autocommit = True @with_lock @with_cursor