session_db: refactor retry handling

pull/2597/head
Stéphane Bidoul 2023-03-24 09:38:44 +01:00
parent 0af0424b4f
commit 2344802039
1 changed files with 16 additions and 10 deletions

View File

@ -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