From 2e302b2d8feca685c8f1b8e2174f239086d239fa Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Tue, 13 Sep 2022 21:47:50 +0200 Subject: Use ``;`` instead of ``select 1`` to ping PostgreSQL Fixes: #8491 Change-Id: I941d2a3cf92e5609e2045a53cec94522340951db --- lib/sqlalchemy/dialects/postgresql/asyncpg.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'lib/sqlalchemy/dialects/postgresql/asyncpg.py') diff --git a/lib/sqlalchemy/dialects/postgresql/asyncpg.py b/lib/sqlalchemy/dialects/postgresql/asyncpg.py index a84bece4f..4cc04d20a 100644 --- a/lib/sqlalchemy/dialects/postgresql/asyncpg.py +++ b/lib/sqlalchemy/dialects/postgresql/asyncpg.py @@ -741,6 +741,12 @@ class AsyncAdapt_asyncpg_connection(AdaptedConnection): else: self.isolation_level = self._isolation_setting + def ping(self): + try: + _ = self.await_(self._connection.fetchrow(";")) + except Exception as error: + self._handle_exception(error) + def set_isolation_level(self, level): if self._started: self.rollback() @@ -996,6 +1002,17 @@ class PGDialect_asyncpg(PGDialect): util.coerce_kw_type(opts, "port", int) return ([], opts) + def do_ping(self, dbapi_connection): + try: + dbapi_connection.ping() + except self.dbapi.Error as err: + if self.is_disconnect(err, dbapi_connection, None): + return False + else: + raise + else: + return True + @classmethod def get_pool_class(cls, url): -- cgit v1.2.1