MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _finalize_fairy

Function _finalize_fairy

lib/sqlalchemy/pool/base.py:915–1044  ·  view source on GitHub ↗

Cleanup 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 functio

(
    dbapi_connection: Optional[DBAPIConnection],
    connection_record: Optional[_ConnectionRecord],
    pool: Pool,
    ref: Optional[
        weakref.ref[_ConnectionFairy]
    ],  # this is None when called directly, not by the gc
    echo: Optional[log._EchoFlagType],
    transaction_was_reset: bool = False,
    fairy: Optional[_ConnectionFairy] = None,
)

Source from the content-addressed store, hash-verified

913
914
915def _finalize_fairy(
916 dbapi_connection: Optional[DBAPIConnection],
917 connection_record: Optional[_ConnectionRecord],
918 pool: Pool,
919 ref: Optional[
920 weakref.ref[_ConnectionFairy]
921 ], # this is None when called directly, not by the gc
922 echo: Optional[log._EchoFlagType],
923 transaction_was_reset: bool = False,
924 fairy: Optional[_ConnectionFairy] = None,
925) -> None:
926 """Cleanup for a :class:`._ConnectionFairy` whether or not it's already
927 been garbage collected.
928
929 When using an async dialect no IO can happen here (without using
930 a dedicated thread), since this is called outside the greenlet
931 context and with an already running loop. In this case function
932 will only log a message and raise a warning.
933 """
934
935 is_gc_cleanup = ref is not None
936
937 if is_gc_cleanup:
938 assert ref is not None
939 _strong_ref_connection_records.pop(ref, None)
940 assert connection_record is not None
941 if connection_record.fairy_ref is not ref:
942 return
943 assert dbapi_connection is None
944 dbapi_connection = connection_record.dbapi_connection
945
946 elif fairy:
947 _strong_ref_connection_records.pop(weakref.ref(fairy), None)
948
949 # null pool is not _is_asyncio but can be used also with async dialects
950 dont_restore_gced = pool._dialect.is_async
951
952 if dont_restore_gced:
953 detach = connection_record is None or is_gc_cleanup
954 can_manipulate_connection = not is_gc_cleanup
955 can_close_or_terminate_connection = (
956 not pool._dialect.is_async or pool._dialect.has_terminate
957 )
958 requires_terminate_for_close = (
959 pool._dialect.is_async and pool._dialect.has_terminate
960 )
961
962 else:
963 detach = connection_record is None
964 can_manipulate_connection = can_close_or_terminate_connection = True
965 requires_terminate_for_close = False
966
967 if dbapi_connection is not None:
968 if connection_record and echo:
969 pool.logger.debug(
970 "Connection %r being returned to pool", dbapi_connection
971 )
972

Callers 3

checkoutMethod · 0.85
_checkinMethod · 0.85

Calls 11

_ConnectionFairyClass · 0.85
debugMethod · 0.80
_close_connectionMethod · 0.80
errorMethod · 0.80
popMethod · 0.45
_resetMethod · 0.45
detachMethod · 0.45
close_detachedMethod · 0.45
invalidateMethod · 0.45
warnMethod · 0.45
checkinMethod · 0.45