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: try:
self._ensure_connection() self._ensure_connection()
return func(self, *args, **kwargs) return func(self, *args, **kwargs)
except (psycopg2.InterfaceError, psycopg2.OperationalError) as e: except (psycopg2.InterfaceError, psycopg2.OperationalError):
_logger.info("Session in DB connection Retry %s/5" % tries) self._close_connection()
if tries > 4: if tries > 4:
raise e _logger.warning(
self._open_connection() "session_db operation try %s/5 failed, aborting", tries
)
raise
_logger.info("session_db operation try %s/5 failed, retrying", tries)
return wrapper return wrapper
@ -65,8 +68,7 @@ class PGSessionStore(sessions.SessionStore):
self._setup_db() self._setup_db()
def __del__(self): def __del__(self):
if self._cr is not None: self._close_connection()
self._cr.close()
@with_lock @with_lock
def _ensure_connection(self): def _ensure_connection(self):
@ -75,16 +77,20 @@ class PGSessionStore(sessions.SessionStore):
@with_lock @with_lock
def _open_connection(self): 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: if self._cr is not None:
try: try:
self._cr.close() self._cr.close()
except Exception: # pylint: disable=except-pass except Exception: # pylint: disable=except-pass
pass pass
self._cr = None 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_lock
@with_cursor @with_cursor