U kf @sUdZddlmZddlmZddlZddlmZddlZddl Z ddl Z ddl m Z ddl m Z ddl m Z dd l mZdd l mZdd l mZdd l mZdd l mZddl mZddl mZddlZddlmZddlmZddlmZddlmZddlmZddlmZerlddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddl%m&Z&ej'ddGd d!d!Z(Gd"d#d#eZ)ee)ed$fZ*e+e)\Z,Z-Z.Gd%d&d&Z/Gd'd(d(e/Z0Gd)d*d*eZ1Gd+d,d,eZ2Gd-d.d.ej3ej4Z5Gd/d0d0Z6Gd1d2d2e6Z7Gd3d4d4e7Z8dHd6d7d.d8d9d:d;dd?Z9iZ:d@e;dA<GdBdCdCe6ZdS)Iz'Base constructs for connection pools. ) annotations)dequeN)Enum)Any)Callable)cast)Deque)Dict)List)Optional)Tuple) TYPE_CHECKING)Union)event)exc)log)util)Literal)Protocol)DBAPIConnection) DBAPICursor)Dialect)_DispatchCommon)_ListenerFnType) dispatcher) _InfoTypeT)frozenc@s.eZdZUdZdZded<ded<ded<dS)PoolResetStatezdescribes the state of a DBAPI connection as it is being passed to the :meth:`.PoolEvents.reset` connection pool event. .. versionadded:: 2.0.0b3 transaction_was_resetterminate_only asyncio_safeboolr r!r"N)__name__ __module__ __qualname____doc__ __slots____annotations__r*r*D/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/pool/base.pyr2s   rc@seZdZdZdZdZdZdS) ResetStylez1Describe options for "reset on return" behaviors.rrN)r$r%r&r'reset_rollback reset_commit reset_noner*r*r*r+r,_sr,)TNFcommitrollbackc@sxeZdZdZdZdZdddddZddddd Zd ddd d Zd ddd dZ d ddddZ d ddddZ dS) _ConnDialectzpartial implementation of :class:`.Dialect` which provides DBAPI connection methods. When a :class:`_pool.Pool` is combined with an :class:`_engine.Engine`, the :class:`_engine.Engine` replaces this with its own :class:`.Dialect`. FPoolProxiedConnectionNone)dbapi_connectionreturncCs |dSN)r2selfr6r*r*r+ do_rollback{sz_ConnDialect.do_rollbackcCs |dSr8)r1r9r*r*r+ do_commit~sz_ConnDialect.do_commitrcCs |dSr8closer9r*r*r+ do_terminatesz_ConnDialect.do_terminatecCs |dSr8r=r9r*r*r+do_closesz_ConnDialect.do_closer#cCs tddS)NzJThe ping feature requires that a dialect is passed to the connection pool.NotImplementedErrorr9r*r*r+_do_ping_w_eventsz_ConnDialect._do_ping_w_eventr) connectionr7cCs|Sr8r*)r:rDr*r*r+get_driver_connectionsz"_ConnDialect.get_driver_connectionN) r$r%r&r'is_async has_terminater;r<r?r@rCrEr*r*r*r+r3ns r3c@seZdZdZdS)_AsyncConnDialectTN)r$r%r&rFr*r*r*r+rHsrHc@seZdZddddZdS)_CreatorFnTyperr7cCsdSr8r*r:r*r*r+__call__z_CreatorFnType.__call__Nr$r%r&rLr*r*r*r+rIsrIc@seZdZdddddZdS)_CreatorWRecFnTypeConnectionPoolEntryr)recr7cCsdSr8r*r:rQr*r*r+rLrMz_CreatorWRecFnType.__call__NrNr*r*r*r+rOsrOc @sreZdZUdZded<ded<ded<eZded <d ed <d ed <ded<dEd ddddddddd ddZej ddddZ e d dd d!Z e j d d"d#d$d!Z e jd"dd%d!Z d d d#d&d'Zdd(d)dd"d*d+d,Zd-dd.d/ZdFd0d1dd"d2d3d4Zddd5d6Zd"dd7d8Zd0dd9d:Zd-d"d;dd?Zd-d"d;d@dAZdBddCdDZdS)GPoolz)Abstract base class for connection pools.zdispatcher[Pool]dispatchlog._EchoFlagTypeechoz Optional[str]_orig_logging_namezUnion[_ConnDialect, Dialect]_dialectz)Union[_CreatorFnType, _CreatorWRecFnType] _creator_argrO_invoke_creatorfloat_invalidate_timeNTFint_ResetStyleArgTypez+Optional[List[Tuple[_ListenerFnType, str]]]z&Optional[Union[_ConnDialect, Dialect]]r#zOptional[_DispatchCommon[Pool]]) creatorrecyclerV logging_namereset_on_returneventsdialectpre_ping _dispatchc Cs|r||_|_nd|_tj||d||_||_d|_||_t |t j ddgt j dddgt j dgid |_||_| r|jj| dd |r||_|r|D]\} } t|| | qdS) a4 Construct a Pool. :param creator: a callable function that returns a DB-API connection object. The function will be called with parameters. :param recycle: If set to a value other than -1, number of seconds between connection recycling, which means upon checkout, if this timeout is surpassed the connection will be closed and replaced with a newly opened connection. Defaults to -1. :param logging_name: String identifier which will be used within the "name" field of logging records generated within the "sqlalchemy.pool" logger. Defaults to a hexstring of the object's id. :param echo: if True, the connection pool will log informational output such as when connections are invalidated as well as when connections are recycled to the default log handler, which defaults to ``sys.stdout`` for output.. If set to the string ``"debug"``, the logging will include pool checkouts and checkins. The :paramref:`_pool.Pool.echo` parameter can also be set from the :func:`_sa.create_engine` call by using the :paramref:`_sa.create_engine.echo_pool` parameter. .. seealso:: :ref:`dbengine_logging` - further detail on how to configure logging. :param reset_on_return: Determine steps to take on connections as they are returned to the pool, which were not otherwise handled by a :class:`_engine.Connection`. Available from :func:`_sa.create_engine` via the :paramref:`_sa.create_engine.pool_reset_on_return` parameter. :paramref:`_pool.Pool.reset_on_return` can have any of these values: * ``"rollback"`` - call rollback() on the connection, to release locks and transaction resources. This is the default value. The vast majority of use cases should leave this value set. * ``"commit"`` - call commit() on the connection, to release locks and transaction resources. A commit here may be desirable for databases that cache query plans if a commit is emitted, such as Microsoft SQL Server. However, this value is more dangerous than 'rollback' because any data changes present on the transaction are committed unconditionally. * ``None`` - don't do anything on the connection. This setting may be appropriate if the database / DBAPI works in pure "autocommit" mode at all times, or if a custom reset handler is established using the :meth:`.PoolEvents.reset` event handler. * ``True`` - same as 'rollback', this is here for backwards compatibility. * ``False`` - same as None, this is here for backwards compatibility. For further customization of reset on return, the :meth:`.PoolEvents.reset` event hook may be used which can perform any connection activity desired on reset. .. seealso:: :ref:`pool_reset_on_return` :meth:`.PoolEvents.reset` :param events: a list of 2-tuples, each of the form ``(callable, target)`` which will be passed to :func:`.event.listen` upon construction. Provided here so that event listeners can be assigned via :func:`_sa.create_engine` before dialect-level listeners are applied. :param dialect: a :class:`.Dialect` that will handle the job of calling rollback(), close(), or commit() on DBAPI connections. If omitted, a built-in "stub" dialect is used. Applications that make use of :func:`_sa.create_engine` should not use this parameter as it is handled by the engine creation strategy. :param pre_ping: if True, the pool will emit a "ping" (typically "SELECT 1", but is dialect-specific) on the connection upon checkout, to test if the connection is alive or not. If not, the connection is transparently re-connected and upon success, all other pooled connections established prior to that timestamp are invalidated. Requires that a dialect is passed as well to interpret the disconnection error. .. versionadded:: 1.2 N)Zechoflagrr2TnoneFr1rc)Zonly_propagate)rbrWrZinstance_logger_creator_recycler\ _pre_pingrZparse_user_argument_for_enumr,r.r0r/_reset_on_returnrVrT_updaterXrlisten) r:r`rarVrbrcrdrerfrgfntargetr*r*r+__init__s6l  z Pool.__init__rJcCs|jjSr8)rXrFrKr*r*r+ _is_asyncio3szPool._is_asynciocCs|jSr8)rYrKr*r*r+ri7sz Pool._creatorr5)r`r7cCs||_|||_dSr8)rY_should_wrap_creatorrZ)r:r`r*r*r+ri;scCs |`|`dSr8)rYrZrKr*r*r+riEscsztj|jdd}Wn*tk r>tt|fddYSX|jdk rVt|j}nd}t|d|}|d|dfdgd fkrtt|S|d krtt|Stt|fd dSdS) zlDetect if creator accepts a single argument, or is sent as a legacy style no-arg function. T)Zno_selfcsSr8r*rQZ creator_fnr*r+WrMz+Pool._should_wrap_creator..Nrconnection_recordr8r-csSr8r*rtrur*r+rvjrM) rZget_callable_argspecri TypeErrorrrIdefaultslenrO)r:r`ZargspecZ defaulted positionalsr*rur+rsKs      zPool._should_wrap_creator terminater)rDr~r7c Cs|jd|rdnd|z"|r,|j|n |j|WnPtk r}z2|jjd|r\dndd|dd t|tszW5d}~XYnXdS) Nz%s connection %rz Hard-closingZClosingz Exception Z terminatingclosingz connection %rTexc_info) loggerdebugrXr?r@ BaseExceptionerror isinstance Exception)r:rDr~er*r*r+_close_connectionls"  zPool._close_connectionrPcCst|S)z6Called by subclasses to create a new ConnectionRecord.)_ConnectionRecordrKr*r*r+_create_connectionszPool._create_connectionr4Optional[BaseException])rD exception_checkinr7cCsDt|dd}|r|j|jkr&t|_|r@t|ddr@||dS)aMark all connections established within the generation of the given connection as invalidated. If this pool's last invalidate time is before when the given connection was created, update the timestamp til now. Otherwise, no action is performed. Connections with a start time prior to this pool's invalidation time will be recycled upon next checkout. _connection_recordNis_validF)getattrr\ starttimetime invalidate)r:rDrrrQr*r*r+ _invalidates   zPool._invalidatecCs tdS)a"Return a new :class:`_pool.Pool`, of the same class as this one and configured with identical creation arguments. This method is used in conjunction with :meth:`dispose` to close out an entire :class:`_pool.Pool` and create a new one in its place. NrArKr*r*r+recreates z Pool.recreatecCs tdS)zDispose of this pool. This method leaves the possibility of checked-out connections remaining open, as it only affects connections that are idle in the pool. .. seealso:: :meth:`Pool.recreate` NrArKr*r*r+disposes z Pool.disposecCs t|S)zReturn a DBAPI connection from the pool. The connection is instrumented such that when its ``close()`` method is called, the connection will be returned to the pool. )_ConnectionFairy _checkoutrKr*r*r+connectsz Pool.connect)recordr7cCs||dS)zGiven a _ConnectionRecord, return it to the :class:`_pool.Pool`. This method is called when an instrumented DBAPI connection has its ``close()`` method called. N)_do_return_connr:rr*r*r+ _return_connszPool._return_conncCs tdS)z7Implementation for :meth:`get`, supplied by subclasses.NrArKr*r*r+_do_getsz Pool._do_getcCs tdS)z?Implementation for :meth:`return_conn`, supplied by subclasses.NrArr*r*r+rszPool._do_return_connstrcCs tdSr8rArKr*r*r+statussz Pool.status)r]NNTNNFN)NT)r$r%r&r'r)r3rXrqrZhybridpropertyrrpropertyrisetterdeleterrsrrrrrrrrrrr*r*r*r+rSsP    "   rSc@sbeZdZUdZdZded<ded<ejddd d Zejd dd d Z dddddddZ dS)ManagesConnectiona3Common base for the two connection-management interfaces :class:`.PoolProxiedConnection` and :class:`.ConnectionPoolEntry`. These two objects are typically exposed in the public facing API via the connection pool event hooks, documented at :class:`.PoolEvents`. .. versionadded:: 2.0 r*Optional[DBAPIConnection]r6 Optional[Any]driver_connectionrrJcCs tdS)aInfo dictionary associated with the underlying DBAPI connection referred to by this :class:`.ManagesConnection` instance, allowing user-defined data to be associated with the connection. The data in this dictionary is persistent for the lifespan of the DBAPI connection itself, including across pool checkins and checkouts. When the connection is invalidated and replaced with a new one, this dictionary is cleared. For a :class:`.PoolProxiedConnection` instance that's not associated with a :class:`.ConnectionPoolEntry`, such as if it were detached, the attribute returns a dictionary that is local to that :class:`.ConnectionPoolEntry`. Therefore the :attr:`.ManagesConnection.info` attribute will always provide a Python dictionary. .. seealso:: :attr:`.ManagesConnection.record_info` NrArKr*r*r+infoszManagesConnection.infoOptional[_InfoType]cCs tdS)aPersistent info dictionary associated with this :class:`.ManagesConnection`. Unlike the :attr:`.ManagesConnection.info` dictionary, the lifespan of this dictionary is that of the :class:`.ConnectionPoolEntry` which owns it; therefore this dictionary will persist across reconnects and connection invalidation for a particular entry in the connection pool. For a :class:`.PoolProxiedConnection` instance that's not associated with a :class:`.ConnectionPoolEntry`, such as if it were detached, the attribute returns None. Contrast to the :attr:`.ManagesConnection.info` dictionary which is never None. .. seealso:: :attr:`.ManagesConnection.info` NrArKr*r*r+ record_info+szManagesConnection.record_infoNFrr#r5rsoftr7cCs tdS)aOMark the managed connection as invalidated. :param e: an exception object indicating a reason for the invalidation. :param soft: if True, the connection isn't closed; instead, this connection will be recycled on next checkout. .. seealso:: :ref:`pool_connection_invalidation` NrAr:rrr*r*r+rCszManagesConnection.invalidate)NF) r$r%r&r'r(r)rro_memoized_propertyrrrr*r*r*r+rs  rc@s4eZdZdZdZeddddZdddd Zd S) rPaInterface for the object that maintains an individual database connection on behalf of a :class:`_pool.Pool` instance. The :class:`.ConnectionPoolEntry` object represents the long term maintainance of a particular connection for a pool, including expiring or invalidating that connection to have it replaced with a new one, which will continue to be maintained by that same :class:`.ConnectionPoolEntry` instance. Compared to :class:`.PoolProxiedConnection`, which is the short-term, per-checkout connection manager, this object lasts for the lifespan of a particular "slot" within a connection pool. The :class:`.ConnectionPoolEntry` object is mostly visible to public-facing API code when it is delivered to connection pool event hooks, such as :meth:`_events.PoolEvents.connect` and :meth:`_events.PoolEvents.checkout`. .. versionadded:: 2.0 :class:`.ConnectionPoolEntry` provides the public facing interface for the :class:`._ConnectionRecord` internal class. r*r#rJcCs tdS)z3Return True the connection is currently checked outNrArKr*r*r+in_usemszConnectionPoolEntry.in_user5cCs tdS)zAClose the DBAPI connection managed by this connection pool entry.NrArKr*r*r+r>sszConnectionPoolEntry.closeN)r$r%r&r'r(rrr>r*r*r*r+rPVs rPc@steZdZUdZdZded<ded<ded<d ed <dEd dd ddZded<eddddZee ddddddZ dZ d ed<e j ddddZe j ddd d!Zed d"d#d$d%ZdFd&dd'd(d)d*ZdGdd'd+d,d-Zeddd.d/Zed dd0d1Zd'dd2d3ZdHd6dd'd7d8d9Zd:dd;d<Zddd=d>Zd5d?dd'd@dAdBZd'ddCdDZd4S)IraMaintains a position in a connection pool which references a pooled connection. This is an internal object used by the :class:`_pool.Pool` implementation to provide context management to a DBAPI connection maintained by that :class:`_pool.Pool`. The public facing interface for this class is described by the :class:`.ConnectionPoolEntry` class. See that class for public API details. .. seealso:: :class:`.ConnectionPoolEntry` :class:`.PoolProxiedConnection` )Z__pool fairy_reffinalize_callbackfreshrr6 __weakref____dict__z(Deque[Callable[[DBAPIConnection], None]]rr#r'Optional[weakref.ref[_ConnectionFairy]]rr[rTrS)poolrcCs6d|_d|_d|_d|_||_|r*|t|_dS)NFr)rrrr6_ConnectionRecord__pool_ConnectionRecord__connectrr)r:rrr*r*r+rqsz_ConnectionRecord.__init__rr6rrJcCs"|jdkrdS|jj|jSdSr8)r6rrXrErKr*r*r+rs  z#_ConnectionRecord.driver_connection2.0zXThe _ConnectionRecord.connection attribute is deprecated; please use 'driver_connection'cCs|jSr8r6rKr*r*r+rDsz_ConnectionRecord.connectionr_soft_invalidate_timercCsiSr8r*rKr*r*r+rsz_ConnectionRecord.inforcCsiSr8r*rKr*r*r+rsz_ConnectionRecord.record_infor)rr7c strttnz }WnFtk rn}z(tj|ddW5QRXW5d}~XYnX t |}t |fdd_ }t|<rjd||S)NF_fairy_was_createdcs tdk rtd|ddSdS)NFr )_finalize_fairy)refrVrrQr*r+rvsz,_ConnectionRecord.checkout..z#Connection %r checked out from pool)r rrrget_connectionrr safe_reraise_checkin_failedZ_should_log_debugrweakrefrr_strong_ref_connection_recordsrr)clsrr6errfairyrr*rr+checkouts,   z_ConnectionRecord.checkoutrr5)rrr7cCs|j|d|j|ddS)Nrr)rcheckin)r:rrr*r*r+rs z!_ConnectionRecord._checkin_failed)rr7cCsx|jdkr |r td|dSd|_|j}|j}|jrT|j}|dk r2||q2|jjrj|j||| |dS)NzDouble checkin attempted on %s) rrwarnr6rrpoprTrr)r:rrDr finalizerr*r*r+rs  z_ConnectionRecord.checkincCs |jdk Sr8)rrKr*r*r+rsz_ConnectionRecord.in_usecCs|jSr8)rrKr*r*r+last_connect_time sz#_ConnectionRecord.last_connect_timecCs|jdk r|dSr8)r6_ConnectionRecord__closerKr*r*r+r>s z_ConnectionRecord.closeNFrrcCs|jdkrdS|r(|jj|j||n|jj|j|||dk rj|jjd|rVdnd|j|jj|n|jjd|r|dnd|j|rt |_ n|j ddd|_dS)Nz*%sInvalidate connection %r (reason: %s:%s)zSoft z%sInvalidate connection %rTr}) r6rrTZsoft_invalidaterrr __class__r$rrrrr*r*r+rs4     z_ConnectionRecord.invalidatercCsd}|jdkr"|j|n|jjdkr\t|j|jjkr\|jjd|jd}nH|jj |jkr|jjd|jd}n"|j |jkr|jjd|jd}|r|j dd|j||jdk st |jS)NFr]z)Connection %r exceeded timeout; recyclingTz=Connection %r invalidated due to pool invalidation; recyclingzCConnection %r invalidated due to local soft invalidation; recyclingr}) r6rclearrrrjrrrr\rrAssertionError)r:rar*r*r+r3s>       z _ConnectionRecord.get_connectioncCs$|jdkp"|jj|jkp"|j|jkSr8)r6rr\rrrKr*r*r+_is_hard_or_soft_invalidatedgs    z._ConnectionRecord._is_hard_or_soft_invalidatedr})r~r7cCsP|j|jjjr&|jj|j||jdk s4t|jj|j|dd|_dS)Nr})rrrrTr>r6rr)r:r~r*r*r+__closens  z_ConnectionRecord.__closec Cs|j}d|_z2t|_|||_}|jd|d|_WnDtk r}z&t |jd|W5QRXW5d}~XYn>X|j j r|j j |j |j||j j |j |j|dS)NzCreated new connection %rTzError on connect(): %s)rr6rrrZrrrrrrrTZ first_connectZ for_modifyZexec_once_unless_exceptionrZ_exec_w_sync_on_first_run)r:rrDrr*r*r+Z __connectxs.   *z_ConnectionRecord.__connect)T)T)T)NF)r$r%r&r'r(r)rqrrr deprecatedrDrrrr classmethodrrrrrr>rrrrrr*r*r*r+rxsL    $!4 rFrOptional[_ConnectionRecord]rzOptional[log._EchoFlagType]r#Optional[_ConnectionFairy]r5)r6rxrrrVr rr7c CsD|dk }|rR|dk stt|d|dk s0t|j|k r>dS|dksJt|j}n|rhtt|d|jj}|r|dkp~|} | } |jj p|jj } |jjo|jj } n|dk} d} } d} |dk r |r|r|j d|zz|s|dk stt||||}|j|kst|j||| | d | rh|r>||_|| rh|jjrZ|j||j|| d WnTtk r}z4|j j d dd |r|j|dt|tsW5d}~XYnXW5| r|r|rd|d| sdndd } |j | t | X|r$|jdk r$||dk r:d|_d|_~~~dS)acCleanup for a :class:`._ConnectionFairy` whether or not it's already been garbage collected. When using an async dialect no IO can happen here (without using a dedicated thread), since this is called outside the greenlet context and with an already running loop. In this case function will only log a message and raise a warning. NTFz$Connection %r being returned to poolzFThe garbage collector is trying to clean up non-checked-in connection z, which will be z*dropped, as it cannot be safely terminatedZ terminatedz. Please ensure that SQLAlchemy pooled connections are returned to the pool explicitly, either by calling ``close()`` or by using appropriate context managers to manage their lifecycle.rr}z!Exception during reset or similarrr)rrrrr6rrrXrFrGrrrrrr_reset_pooldetachrTZclose_detachedrrrrrrr)r6rxrrrVr rZ is_gc_cleanupZdont_restore_gcedrZcan_manipulate_connectionZ!can_close_or_terminate_connectionZrequires_terminate_for_closemessagerr*r*r+rs                rz6Dict[weakref.ref[_ConnectionFairy], _ConnectionRecord]rc@seZdZdZdZejr@ddddZdddd Zddd d Z e d dd dZ e d dddZ ddddZ ddddZdS)r4aA connection-like adapter for a :pep:`249` DBAPI connection, which includes additional methods specific to the :class:`.Pool` implementation. :class:`.PoolProxiedConnection` is the public-facing interface for the internal :class:`._ConnectionFairy` implementation object; users familiar with :class:`._ConnectionFairy` can consider this object to be equivalent. .. versionadded:: 2.0 :class:`.PoolProxiedConnection` provides the public- facing interface for the :class:`._ConnectionFairy` internal class. r*r5rJcCsdSr8r*rKr*r*r+r13rMzPoolProxiedConnection.commitrcCsdSr8r*rKr*r*r+cursor5rMzPoolProxiedConnection.cursorcCsdSr8r*rKr*r*r+r27rMzPoolProxiedConnection.rollbackr#cCs tdS)zgReturn True if this :class:`.PoolProxiedConnection` still refers to an active DBAPI connection.NrArKr*r*r+r9szPoolProxiedConnection.is_validcCs tdS)zVReturn True if this :class:`.PoolProxiedConnection` is detached from its pool.NrArKr*r*r+ is_detached@sz!PoolProxiedConnection.is_detachedcCs tdS)aSeparate this connection from its Pool. This means that the connection will no longer be returned to the pool when closed, and will instead be literally closed. The associated :class:`.ConnectionPoolEntry` is de-associated from this DBAPI connection. Note that any overall connection limiting constraints imposed by a Pool implementation may be violated after a detach, as the detached connection is removed from the pool's knowledge and control. NrArKr*r*r+rGszPoolProxiedConnection.detachcCs tdS)a$Release this connection back to the pool. The :meth:`.PoolProxiedConnection.close` method shadows the :pep:`249` ``.close()`` method, altering its behavior to instead :term:`release` the proxied connection back to the connection pool. Upon release to the pool, whether the connection stays "opened" and pooled in the Python process, versus actually closed out and removed from the Python process, is based on the pool implementation in use and its configuration and current state. NrArKr*r*r+r>Ws zPoolProxiedConnection.closeN)r$r%r&r'r(typingr r1rr2rrrrr>r*r*r*r+r4"s r4c@seZdZUdZdZded<ded<ddddd Zed d d d Zedd ddZ edd ddZ d$ddddddZ e j dd ddZd d dddd Zd d d!d"d#ZdS)%_AdhocProxiedConnectionadprovides the :class:`.PoolProxiedConnection` interface for cases where the DBAPI connection is not actually proxied. This is used by the engine internals to pass a consistent :class:`.PoolProxiedConnection` object to consuming dialects in response to pool events that may not always have the :class:`._ConnectionFairy` available. r6r _is_validrr6rPr)r6rxcCs||_||_d|_dS)NTr)r:r6rxr*r*r+rqwsz _AdhocProxiedConnection.__init__rrJcCs|jjSr8rrrKr*r*r+rsz)_AdhocProxiedConnection.driver_connectioncCs|jSr8rrKr*r*r+rDsz"_AdhocProxiedConnection.connectionr#cCs|jS)zImplement is_valid state attribute. for the adhoc proxied connection it's assumed the connection is valid as there is no "invalidate" routine. rrKr*r*r+rsz _AdhocProxiedConnection.is_validNFrr5rcCs d|_dS)NFrrr*r*r+rsz"_AdhocProxiedConnection.invalidatercCs|jjSr8rrrKr*r*r+rsz#_AdhocProxiedConnection.record_inforargskwargsr7cOs|jj||Sr8)r6rr:rrr*r*r+rsz_AdhocProxiedConnection.cursorkeyr7cCs t|j|Sr8rr6r:rr*r*r+ __getattr__sz#_AdhocProxiedConnection.__getattr__)NF)r$r%r&r'r(r)rqrrrDrrrro_non_memoized_propertyrrrr*r*r*r+rgs$    rc@seZdZUdZdZded<ded<ded<ddd dd d d Zd ed<eddddZee ddddddZ e dKdddddddZ ddddZdLd d!d"d#d$Zd!dd%d&Zdd d d d!d'd(d)Zed*dd+d,Zed dd-d.Zed dd/d0Ze jd1dd2d3Ze jd4dd5d6ZdMd7d d!d8d9d:Zd;d;dd?Zd@d;dAdBdCZd!ddDdEZd!ddFdGZdNd d!dHdIdJZdS)Ora&Proxies a DBAPI connection and provides return-on-dereference support. This is an internal object used by the :class:`_pool.Pool` implementation to provide context management to a DBAPI connection delivered by that :class:`_pool.Pool`. The public facing interface for this class is described by the :class:`.PoolProxiedConnection` class. See that class for public API details. The name "fairy" is inspired by the fact that the :class:`._ConnectionFairy` object's lifespan is transitory, as it lasts only for the length of a specific DBAPI connection being checked out from the pool, and additionally that as a transparent proxy, it is mostly invisible. .. seealso:: :class:`.PoolProxiedConnection` :class:`.ConnectionPoolEntry` )r6r_echor_counterrrrSrrr6rUrr)rr6rxrVcCs"||_d|_||_||_||_dS)Nr)rrr6rr)r:rr6rxrVr*r*r+rqs z_ConnectionFairy.__init__rrrrJcCs|jdkrdS|jjSr8rrKr*r*r+rs z"_ConnectionFairy.driver_connectionrzWThe _ConnectionFairy.connection attribute is deprecated; please use 'driver_connection'cCs|jSr8rrKr*r*r+rDsz_ConnectionFairy.connectionNzOptional[threading.local]r)r threadconnsrr7c Csl|s"t|}|dk r"t||_|jdk s4td|jdk sFtd|jd7_|j jsb|j rl|jdkrp|Sd}|dkrJ|jj }d|j_ z|j r|s|j r|j d|j|j|j}|s|j r|j d|jtn|j r|j d |j|j |j|j||WStjk r}z|jrZ|j d ||j||j||dd n|j d |j||j|z|j|_WnLtk r}z,t|jj|d d~W5QRXW5d}~XYnX|d8}W5d}~XYqttk rF} z:t&|j} | dk r(| j| d d~W5QRXW5d} ~ XYqtXqt|j d|tddS)Nz,can't 'checkout' a detached connection fairyz0can't 'checkout' an invalidated connection fairyr-rrFzPool pre-ping on connection %sz;Pool pre-ping on connection %s failed, will invalidate poolz)Connection %s is fresh, skipping pre-pingzoDisconnection detected on checkout, invalidating all pooled connections prior to current timestamp (reason: %r)rzVDisconnection detected on checkout, invalidating individual connection %s (reason: %r)Trz+Reconnection attempts exhausted on checkoutzThis connection is closed)rrrrcurrentrrr6rrTrkrrrrrXrCrZInvalidatePoolErrorZDisconnectionErrorZinvalidate_poolrrrrrrrrZInvalidRequestError) rrrrZattemptsZconnection_is_freshresultrrZbe_outerrQr*r*r+rs            z_ConnectionFairy._checkoutcCstj|j|dS)N)r)rrrrKr*r*r+_checkout_existingesz#_ConnectionFairy._checkout_existingFr#r5)r r7c Cs"t|j|j|jd|j||ddS)N)r r)rr6rrr)r:r r*r*r+rhsz_ConnectionFairy._checkincCs |dSr8rrKr*r*r+_closessz_ConnectionFairy._close)rr r!r"r7c Cs|jjr&|j|j|jt|||d|s.dS|jtkrx|rT|jrv|j d|jq|jrj|j d|j|j |n,|jt kr|jr|j d|j|j |dS)Nrz.Connection %s reset, transaction already resetz Connection %s rollback-on-returnzConnection %s commit-on-return)rTresetr6rrrlr.rrrrXr;r/r<)r:rr r!r"r*r*r+rvs@   z_ConnectionFairy._resetzlog._IdentifiedLoggerTypecCs|jjSr8)rrrKr*r*r+_loggersz_ConnectionFairy._loggercCs |jdk Sr8rrKr*r*r+rsz_ConnectionFairy.is_validcCs |jdkSr8)rrKr*r*r+rsz_ConnectionFairy.is_detachedrcCs|jdkriS|jjSdSr8)rrrKr*r*r+rs z_ConnectionFairy.inforcCs|jdkrdS|jjSdSr8rrKr*r*r+rs z_ConnectionFairy.record_inforrcCsD|jdkrtddS|jr.|jj||d|s@d|_|dS)Nz.Can't invalidate an already-closed connection.)rr)r6rrrrrrr*r*r+rs  z_ConnectionFairy.invalidaterrrcOs|jdk st|jj||Sr8)r6rrrr*r*r+rsz_ConnectionFairy.cursorrrcCs t|j|Sr8rrr*r*r+rsz_ConnectionFairy.__getattr__cCs\|jdk rX|j}d|_d|_|j|j|j|_d|_|jjjrX|jj|j|dSr8) rrr6rrrcopyrTrrRr*r*r+rs   z_ConnectionFairy.detachcCs$|jd8_|jdkr |dS)Nr-rrrrKr*r*r+r>s z_ConnectionFairy.close)transaction_resetr7cCs(|jd8_|jdkr$|j|ddS)Nr-rrr)r:rr*r*r+_close_specials z_ConnectionFairy._close_special)NN)F)NF)F)r$r%r&r'r(r)rqrrrrrDrrrrrrrrrrrrrrrrrr>rr*r*r*r+rsR   } +r)FN)?r' __future__r collectionsr dataclassesenumr threadingrrrrrrr r r r r rrrrrrrZ util.typingrrZengine.interfacesrrrrrrZ sql._typingr dataclassrr,r_listr.r/r0r3rHrIrOZ IdentifiedZ EventTargetrSrrPrrrr)r4rrr*r*r*r+ s|                           ,#?|"'  E;