(
cls,
pool: Pool,
threadconns: Optional[threading.local] = None,
fairy: Optional[_ConnectionFairy] = None,
)
| 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", |
no test coverage detected