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