session_db: reconnect if needed

If the connection to the database fails when
retrying a session operation, we end up
with no cursore, which makes subsequent
session operations fail.

We fix this by ensuring we have cursor
before any operations.
pull/2597/head
Stéphane Bidoul 2023-03-23 17:36:07 +01:00
parent 1b5e684023
commit 13c570d639
1 changed files with 6 additions and 0 deletions

View File

@ -45,6 +45,7 @@ def with_cursor(func):
while True:
tries += 1
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)
@ -67,6 +68,11 @@ class PGSessionStore(sessions.SessionStore):
if self._cr is not None:
self._cr.close()
@with_lock
def _ensure_connection(self):
if self._cr is None:
self._open_connection()
@with_lock
def _open_connection(self):
# return cursor to the pool