diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2023-02-24 16:15:21 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2023-03-01 16:20:36 -0500 |
| commit | 45d0a501609e3588f1accac59c08358c4c6c74a1 (patch) | |
| tree | c97aafa7c5d1709ff1af8fe9717bd6220c35d0d9 /lib/sqlalchemy/pool | |
| parent | 8b108297d075ae68178cd18a9cb4d06feee7e075 (diff) | |
| download | sqlalchemy-ticket_5648.tar.gz | |
ensure event handlers called for all do_pingticket_5648
The support for pool ping listeners to receive exception events via the
:meth:`.ConnectionEvents.handle_error` event added in 2.0.0b1 for
:ticket:`5648` failed to take into account dialect-specific ping routines
such as that of MySQL and PostgreSQL. The dialect feature has been reworked
so that all dialects participate within event handling. Additionally,
a new boolean element :attr:`.ExceptionContext.is_pre_ping` is added
which identifies if this operation is occurring within the pre-ping
operation.
For this release, third party dialects which implement a custom
:meth:`_engine.Dialect.do_ping` method can opt in to the newly improved
behavior by having their method no longer catch exceptions or check
exceptions for "is_disconnect", instead just propagating all exceptions
outwards. Checking the exception for "is_disconnect" is now done by an
enclosing method on the default dialect, which ensures that the event hook
is invoked for all exception scenarios before testing the exception as a
"disconnect" exception. If an existing ``do_ping()`` method continues to
catch exceptions and check "is_disconnect", it will continue to work as it
did previously, but ``handle_error`` hooks will not have access to the
exception if it isn't propagated outwards.
Fixes: #5648
Change-Id: I6535d5cb389e1a761aad8c37cfeb332c548b876d
Diffstat (limited to 'lib/sqlalchemy/pool')
| -rw-r--r-- | lib/sqlalchemy/pool/base.py | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/lib/sqlalchemy/pool/base.py b/lib/sqlalchemy/pool/base.py index d67f32442..ac487452c 100644 --- a/lib/sqlalchemy/pool/base.py +++ b/lib/sqlalchemy/pool/base.py @@ -132,7 +132,7 @@ class _ConnDialect: def do_close(self, dbapi_connection: DBAPIConnection) -> None: dbapi_connection.close() - def do_ping(self, dbapi_connection: DBAPIConnection) -> bool: + def _do_ping_w_event(self, dbapi_connection: DBAPIConnection) -> bool: raise NotImplementedError( "The ping feature requires that a dialect is " "passed to the connection pool." @@ -1266,6 +1266,7 @@ class _ConnectionFairy(PoolProxiedConnection): threadconns: Optional[threading.local] = None, fairy: Optional[_ConnectionFairy] = None, ) -> _ConnectionFairy: + if not fairy: fairy = _ConnectionRecord.checkout(pool) @@ -1304,7 +1305,9 @@ class _ConnectionFairy(PoolProxiedConnection): "Pool pre-ping on connection %s", fairy.dbapi_connection, ) - result = pool._dialect.do_ping(fairy.dbapi_connection) + result = pool._dialect._do_ping_w_event( + fairy.dbapi_connection + ) if not result: if fairy._echo: pool.logger.debug( |
