diff --git a/pyproject.toml b/pyproject.toml index 201f5ac..3ad249d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "rda_python_common" -version = "3.0.0" +version = "3.0.1" authors = [ { name="Zaihua Ji", email="zji@ucar.edu" }, ] diff --git a/src/rda_python_common/PgDBI.py b/src/rda_python_common/PgDBI.py index e83a819..00f9597 100644 --- a/src/rda_python_common/PgDBI.py +++ b/src/rda_python_common/PgDBI.py @@ -328,8 +328,12 @@ def starttran(): pgconnect(0, 0, False) else: try: - pgdb.isolation_level - except PgSQL.OperationalError as e: + # Liveness probe: psycopg2's isolation_level getter could raise on a + # dead connection, but psycopg3's is a cached attribute that never + # touches the server. A trivial round-trip detects a broken + # connection under either driver. + pgdb.cursor().execute("SELECT 1") + except PgSQL.Error as e: pgconnect(0, 0, False) if pgdb.closed: pgconnect(0, 0, False) diff --git a/src/rda_python_common/pg_dbi.py b/src/rda_python_common/pg_dbi.py index 4858e8d..5880c27 100644 --- a/src/rda_python_common/pg_dbi.py +++ b/src/rda_python_common/pg_dbi.py @@ -410,8 +410,12 @@ def starttran(self): self.pgconnect(0, 0, False) else: try: - self.pgdb.isolation_level - except PgSQL.OperationalError as e: + # Liveness probe: psycopg2's isolation_level getter could raise on a + # dead connection, but psycopg3's is a cached attribute that never + # touches the server. A trivial round-trip detects a broken + # connection under either driver. + self.pgdb.cursor().execute("SELECT 1") + except PgSQL.Error as e: self.pgconnect(0, 0, False) if self.pgdb.closed: self.pgconnect(0, 0, False) @@ -533,7 +537,11 @@ def try_add_table(self, dberror, logact): dberror (str): Full database error string to inspect. logact (int): Logging action flags forwarded to add_new_table(). """ - ms = re.match(r'^42P01 ERROR: relation "(.+)" does not exist', dberror) + # The caller (check_dberror) only invokes this after pgcode == '42P01', so + # match just the relation message. psycopg2's pgerror includes the + # 'ERROR: ' severity prefix while psycopg3's message_primary does not, so + # anchoring on that prefix only matched under psycopg2. + ms = re.search(r'relation "(.+)" does not exist', dberror) if ms: tname = ms.group(1) self.add_new_table(tname, logact = logact)