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