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/engine | |
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/engine')
-rw-r--r-- | lib/sqlalchemy/engine/base.py | 6 | ||||
-rw-r--r-- | lib/sqlalchemy/engine/default.py | 27 | ||||
-rw-r--r-- | lib/sqlalchemy/engine/interfaces.py | 14 |
3 files changed, 34 insertions, 13 deletions
diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index f6c637aa8..09610b069 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -2275,6 +2275,7 @@ class Connection(ConnectionEventsTarget, inspection.Inspectable["Inspector"]): context, self._is_disconnect, invalidate_pool_on_disconnect, + False, ) for fn in self.dialect.dispatch.handle_error: @@ -2345,6 +2346,7 @@ class Connection(ConnectionEventsTarget, inspection.Inspectable["Inspector"]): engine: Optional[Engine] = None, is_disconnect: Optional[bool] = None, invalidate_pool_on_disconnect: bool = True, + is_pre_ping: bool = False, ) -> NoReturn: exc_info = sys.exc_info() @@ -2385,6 +2387,7 @@ class Connection(ConnectionEventsTarget, inspection.Inspectable["Inspector"]): None, is_disconnect, invalidate_pool_on_disconnect, + is_pre_ping, ) for fn in dialect.dispatch.handle_error: try: @@ -2443,6 +2446,7 @@ class ExceptionContextImpl(ExceptionContext): "execution_context", "is_disconnect", "invalidate_pool_on_disconnect", + "is_pre_ping", ) def __init__( @@ -2458,6 +2462,7 @@ class ExceptionContextImpl(ExceptionContext): context: Optional[ExecutionContext], is_disconnect: bool, invalidate_pool_on_disconnect: bool, + is_pre_ping: bool, ): self.engine = engine self.dialect = dialect @@ -2469,6 +2474,7 @@ class ExceptionContextImpl(ExceptionContext): self.parameters = parameters self.is_disconnect = is_disconnect self.invalidate_pool_on_disconnect = invalidate_pool_on_disconnect + self.is_pre_ping = is_pre_ping class Transaction(TransactionalContext): diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py index f8126fa30..3e4e6fb9a 100644 --- a/lib/sqlalchemy/engine/default.py +++ b/lib/sqlalchemy/engine/default.py @@ -669,16 +669,11 @@ class DefaultDialect(Dialect): def _dialect_specific_select_one(self): return str(expression.select(1).compile(dialect=self)) - def do_ping(self, dbapi_connection: DBAPIConnection) -> bool: - cursor = None + def _do_ping_w_event(self, dbapi_connection: DBAPIConnection) -> bool: try: - cursor = dbapi_connection.cursor() - try: - cursor.execute(self._dialect_specific_select_one) - finally: - cursor.close() + return self.do_ping(dbapi_connection) except self.loaded_dbapi.Error as err: - is_disconnect = self.is_disconnect(err, dbapi_connection, cursor) + is_disconnect = self.is_disconnect(err, dbapi_connection, None) if self._has_events: try: @@ -687,19 +682,25 @@ class DefaultDialect(Dialect): self, is_disconnect=is_disconnect, invalidate_pool_on_disconnect=False, + is_pre_ping=True, ) except exc.StatementError as new_err: is_disconnect = new_err.connection_invalidated - # other exceptions modified by the event handler will be - # thrown - if is_disconnect: return False else: raise - else: - return True + + def do_ping(self, dbapi_connection: DBAPIConnection) -> bool: + cursor = None + + cursor = dbapi_connection.cursor() + try: + cursor.execute(self._dialect_specific_select_one) + finally: + cursor.close() + return True def create_xid(self): """Create a random two-phase transaction ID. diff --git a/lib/sqlalchemy/engine/interfaces.py b/lib/sqlalchemy/engine/interfaces.py index c1de13221..9952a85e3 100644 --- a/lib/sqlalchemy/engine/interfaces.py +++ b/lib/sqlalchemy/engine/interfaces.py @@ -1967,6 +1967,9 @@ class Dialect(EventTarget): raise NotImplementedError() + def _do_ping_w_event(self, dbapi_connection: DBAPIConnection) -> bool: + raise NotImplementedError() + def do_ping(self, dbapi_connection: DBAPIConnection) -> bool: """ping the DBAPI connection and return True if the connection is usable.""" @@ -3291,6 +3294,17 @@ class ExceptionContext: """ + is_pre_ping: bool + """Indicates if this error is occurring within the "pre-ping" step + performed when :paramref:`_sa.create_engine.pool_pre_ping` is set to + ``True``. In this mode, the :attr:`.ExceptionContext.engine` attribute + will be ``None``. The dialect in use is accessible via the + :attr:`.ExceptionContext.dialect` attribute. + + .. versionadded:: 2.0.5 + + """ + class AdaptedConnection: """Interface of an adapted connection object to support the DBAPI protocol. |