MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / _checkout

Method _checkout

lib/sqlalchemy/pool/base.py:1265–1388  ·  view source on GitHub ↗
(
        cls,
        pool: Pool,
        threadconns: Optional[threading.local] = None,
        fairy: Optional[_ConnectionFairy] = None,
    )

Source from the content-addressed store, hash-verified

1263
1264 @classmethod
1265 def _checkout(
1266 cls,
1267 pool: Pool,
1268 threadconns: Optional[threading.local] = None,
1269 fairy: Optional[_ConnectionFairy] = None,
1270 ) -> _ConnectionFairy:
1271 if not fairy:
1272 fairy = _ConnectionRecord.checkout(pool)
1273
1274 if threadconns is not None:
1275 threadconns.current = weakref.ref(fairy)
1276
1277 assert (
1278 fairy._connection_record is not None
1279 ), "can't 'checkout' a detached connection fairy"
1280 assert (
1281 fairy.dbapi_connection is not None
1282 ), "can't 'checkout' an invalidated connection fairy"
1283
1284 fairy._counter += 1
1285 if (
1286 not pool.dispatch.checkout and not pool._pre_ping
1287 ) or fairy._counter != 1:
1288 return fairy
1289
1290 # Pool listeners can trigger a reconnection on checkout, as well
1291 # as the pre-pinger.
1292 # there are three attempts made here, but note that if the database
1293 # is not accessible from a connection standpoint, those won't proceed
1294 # here.
1295
1296 attempts = 2
1297
1298 while attempts > 0:
1299 connection_is_fresh = fairy._connection_record.fresh
1300 fairy._connection_record.fresh = False
1301 try:
1302 if pool._pre_ping:
1303 if not connection_is_fresh:
1304 if fairy._echo:
1305 pool.logger.debug(
1306 "Pool pre-ping on connection %s",
1307 fairy.dbapi_connection,
1308 )
1309 result = pool._dialect._do_ping_w_event(
1310 fairy.dbapi_connection
1311 )
1312 if not result:
1313 if fairy._echo:
1314 pool.logger.debug(
1315 "Pool pre-ping on connection %s failed, "
1316 "will invalidate pool",
1317 fairy.dbapi_connection,
1318 )
1319 raise exc.InvalidatePoolError()
1320 elif fairy._echo:
1321 pool.logger.debug(
1322 "Connection %s is fresh, skipping pre-ping",

Callers 3

connectMethod · 0.80
_checkout_existingMethod · 0.80
connectMethod · 0.80

Calls 8

debugMethod · 0.80
_invalidateMethod · 0.80
get_connectionMethod · 0.80
_checkin_failedMethod · 0.80
checkoutMethod · 0.45
_do_ping_w_eventMethod · 0.45
infoMethod · 0.45
invalidateMethod · 0.45

Tested by

no test coverage detected