fix(db): validate cached project connections
This commit is contained in:
@@ -20,6 +20,17 @@ def _close_connection(connection: pg.Connection) -> None:
|
||||
connection.close()
|
||||
|
||||
|
||||
def _is_healthy(connection: pg.Connection) -> bool:
|
||||
if _is_closed(connection):
|
||||
return False
|
||||
try:
|
||||
with connection.cursor() as cur:
|
||||
cur.execute("SELECT 1")
|
||||
except pg.Error:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _get_project_lock(name: str) -> RLock:
|
||||
with _registry_lock:
|
||||
lock = _project_locks.get(name)
|
||||
@@ -32,7 +43,7 @@ def _get_project_lock(name: str) -> RLock:
|
||||
def open_connection(name: str) -> pg.Connection:
|
||||
with _get_project_lock(name):
|
||||
connection = g_conn_dict.get(name)
|
||||
if connection is None or _is_closed(connection):
|
||||
if connection is None or not _is_healthy(connection):
|
||||
if connection is not None:
|
||||
_close_connection(connection)
|
||||
connection = pg.connect(
|
||||
@@ -47,8 +58,9 @@ def is_connection_open(name: str) -> bool:
|
||||
connection = g_conn_dict.get(name)
|
||||
if connection is None:
|
||||
return False
|
||||
if _is_closed(connection):
|
||||
if not _is_healthy(connection):
|
||||
del g_conn_dict[name]
|
||||
_close_connection(connection)
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user