diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-06-11 10:23:51 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-06-11 10:34:53 -0400 |
commit | 52feba23f466ca95dfe8cd10d35b546a05b35cbf (patch) | |
tree | 0a327c937fb6cfc3a8b9f28ee5dc48d17130b64c /lib/sqlalchemy/dialects/postgresql | |
parent | ad1c81a6df4950788db3f8007331cac0f9365d97 (diff) | |
download | sqlalchemy-52feba23f466ca95dfe8cd10d35b546a05b35cbf.tar.gz |
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
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql')
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/psycopg2.py | 19 |
1 files changed, 19 insertions, 0 deletions
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() |