From 52feba23f466ca95dfe8cd10d35b546a05b35cbf Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 11 Jun 2021 10:23:51 -0400 Subject: set autocommit for psycopg2 pre-ping Fixed issue where the pool "pre ping" feature would implicitly start a transaction, which would then interfere with custom transactional flags such as PostgreSQL's "read only" mode when used with the psycopg2 driver. Fixes: #6621 Change-Id: I29117c393e50c090cc2587efcccfe1e986738928 --- lib/sqlalchemy/dialects/postgresql/psycopg2.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'lib/sqlalchemy/dialects') diff --git a/lib/sqlalchemy/dialects/postgresql/psycopg2.py b/lib/sqlalchemy/dialects/postgresql/psycopg2.py index 3ef01e9e9..19ca14265 100644 --- a/lib/sqlalchemy/dialects/postgresql/psycopg2.py +++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.py @@ -844,6 +844,25 @@ class PGDialect_psycopg2(PGDialect): def get_deferrable(self, connection): return connection.deferrable + def do_ping(self, dbapi_connection): + cursor = None + try: + dbapi_connection.autocommit = True + cursor = dbapi_connection.cursor() + try: + cursor.execute(self._dialect_specific_select_one) + finally: + cursor.close() + if not dbapi_connection.closed: + dbapi_connection.autocommit = False + except self.dbapi.Error as err: + if self.is_disconnect(err, dbapi_connection, cursor): + return False + else: + raise + else: + return True + def on_connect(self): extras = self._psycopg2_extras() extensions = self._psycopg2_extensions() -- cgit v1.2.1