U kf[3@sddlmZddlZddlmZddlmZddlmZddlmZddlmZdd lm Z dd lm Z dd lm Z d d l m Z d dl mZejrd dlmZd dlmZGddde je ZdS)) annotationsN)Any)Optional)Type)Union)ConnectionPoolEntry)Pool)PoolProxiedConnection)PoolResetState)event)util)Engine)DBAPIConnectionc@seZdZdZdZeZede dddddd Z e d d d d ddZ ddd dddZ ddd dddZ dddd dddZddd dddZeddd gd!d"ddd#d d$d%d&Zddd'd d(d)d*Zddd'd d(d+d,Zddd dd-d.Zddd dd/d0Zdd d1d2d3Zd4S)5 PoolEventsaAvailable events for :class:`_pool.Pool`. The methods here define the name of an event as well as the names of members that are passed to listener functions. e.g.:: from sqlalchemy import event def my_on_checkout(dbapi_conn, connection_rec, connection_proxy): "handle an on checkout event" event.listen(Pool, 'checkout', my_on_checkout) In addition to accepting the :class:`_pool.Pool` class and :class:`_pool.Pool` instances, :class:`_events.PoolEvents` also accepts :class:`_engine.Engine` objects and the :class:`_engine.Engine` class as targets, which will be resolved to the ``.pool`` attribute of the given engine or the :class:`_pool.Pool` class:: engine = create_engine("postgresql+psycopg2://scott:tiger@localhost/test") # will associate with engine.pool event.listen(engine, 'checkout', my_on_checkout) ZSomeEngineOrPoolzsqlalchemy.enginez-Union[Pool, Type[Pool], Engine, Type[Engine]]strz!Optional[Union[Pool, Type[Pool]]])target identifierreturncCsvtjstjjj}t|trszPoolEvents.r )r)r*r2rcCsdS)aCalled before the "reset" action occurs for a pooled connection. This event represents when the ``rollback()`` method is called on the DBAPI connection before it is returned to the pool or discarded. A custom "reset" strategy may be implemented using this event hook, which may also be combined with disabling the default "reset" behavior using the :paramref:`_pool.Pool.reset_on_return` parameter. The primary difference between the :meth:`_events.PoolEvents.reset` and :meth:`_events.PoolEvents.checkin` events are that :meth:`_events.PoolEvents.reset` is called not just for pooled connections that are being returned to the pool, but also for connections that were detached using the :meth:`_engine.Connection.detach` method as well as asyncio connections that are being discarded due to garbage collection taking place on connections before the connection was checked in. Note that the event **is not** invoked for connections that were invalidated using :meth:`_engine.Connection.invalidate`. These events may be intercepted using the :meth:`.PoolEvents.soft_invalidate` and :meth:`.PoolEvents.invalidate` event hooks, and all "connection close" events may be intercepted using :meth:`.PoolEvents.close`. The :meth:`_events.PoolEvents.reset` event is usually followed by the :meth:`_events.PoolEvents.checkin` event, except in those cases where the connection is discarded immediately after reset. :param dbapi_connection: a DBAPI connection. The :attr:`.ConnectionPoolEntry.dbapi_connection` attribute. :param connection_record: the :class:`.ConnectionPoolEntry` managing the DBAPI connection. :param reset_state: :class:`.PoolResetState` instance which provides information about the circumstances under which the connection is being reset. .. versionadded:: 2.0 .. seealso:: :ref:`pool_reset_on_return` :meth:`_events.ConnectionEvents.rollback` :meth:`_events.ConnectionEvents.commit` Nr!)r,r)r*r2r!r!r"resetszPoolEvents.resetzOptional[BaseException])r)r* exceptionrcCsdS)a!Called when a DBAPI connection is to be "invalidated". This event is called any time the :meth:`.ConnectionPoolEntry.invalidate` method is invoked, either from API usage or via "auto-invalidation", without the ``soft`` flag. The event occurs before a final attempt to call ``.close()`` on the connection occurs. :param dbapi_connection: a DBAPI connection. The :attr:`.ConnectionPoolEntry.dbapi_connection` attribute. :param connection_record: the :class:`.ConnectionPoolEntry` managing the DBAPI connection. :param exception: the exception object corresponding to the reason for this invalidation, if any. May be ``None``. .. seealso:: :ref:`pool_connection_invalidation` Nr!r,r)r*r5r!r!r" invalidateszPoolEvents.invalidatecCsdS)aNCalled when a DBAPI connection is to be "soft invalidated". This event is called any time the :meth:`.ConnectionPoolEntry.invalidate` method is invoked with the ``soft`` flag. Soft invalidation refers to when the connection record that tracks this connection will force a reconnect after the current connection is checked in. It does not actively close the dbapi_connection at the point at which it is called. :param dbapi_connection: a DBAPI connection. The :attr:`.ConnectionPoolEntry.dbapi_connection` attribute. :param connection_record: the :class:`.ConnectionPoolEntry` managing the DBAPI connection. :param exception: the exception object corresponding to the reason for this invalidation, if any. May be ``None``. Nr!r6r!r!r"soft_invalidateszPoolEvents.soft_invalidatecCsdS)aCalled when a DBAPI connection is closed. The event is emitted before the close occurs. The close of a connection can fail; typically this is because the connection is already closed. If the close operation fails, the connection is discarded. The :meth:`.close` event corresponds to a connection that's still associated with the pool. To intercept close events for detached connections use :meth:`.close_detached`. :param dbapi_connection: a DBAPI connection. The :attr:`.ConnectionPoolEntry.dbapi_connection` attribute. :param connection_record: the :class:`.ConnectionPoolEntry` managing the DBAPI connection. Nr!r+r!r!r"close;szPoolEvents.closecCsdS)aCalled when a DBAPI connection is "detached" from a pool. This event is emitted after the detach occurs. The connection is no longer associated with the given connection record. :param dbapi_connection: a DBAPI connection. The :attr:`.ConnectionPoolEntry.dbapi_connection` attribute. :param connection_record: the :class:`.ConnectionPoolEntry` managing the DBAPI connection. Nr!r+r!r!r"detachTszPoolEvents.detach)r)rcCsdS)aCalled when a detached DBAPI connection is closed. The event is emitted before the close occurs. The close of a connection can fail; typically this is because the connection is already closed. If the close operation fails, the connection is discarded. :param dbapi_connection: a DBAPI connection. The :attr:`.ConnectionPoolEntry.dbapi_connection` attribute. Nr!)r,r)r!r!r"close_detachedfszPoolEvents.close_detachedN)__name__ __module__ __qualname____doc__Z_target_class_docr Z_dispatch_targetrZpreload_module classmethodr#r(r-r.r0r1r Z_legacy_signaturer4r7r8r9r:r;r!r!r!r"rs. 8r) __future__rrrrrrbaserr r r r rrrrZengine.interfacesrZEventsrr!r!r!r"s