MCPcopy Create free account
hub / github.com/pgadmin-org/pgadmin4 / ping

Method ping

web/pgadmin/utils/driver/psycopg3/connection.py:1509–1546  ·  view source on GitHub ↗

Check if the connection is actually alive by executing a lightweight query. Unlike connected(), which only inspects local state, this sends traffic to the server and will detect stale / half-open TCP connections that were silently dropped by firewalls or the OS whil

(self)

Source from the content-addressed store, hash-verified

1507 return self.__async_query_error
1508
1509 def ping(self):
1510 """
1511 Check if the connection is actually alive by executing a lightweight
1512 query. Unlike connected(), which only inspects local state, this
1513 sends traffic to the server and will detect stale / half-open TCP
1514 connections that were silently dropped by firewalls or the OS while
1515 pgAdmin was idle.
1516
1517 Returns True if alive, False otherwise.
1518 """
1519 if not self.connected():
1520 return False
1521
1522 try:
1523 # Check the transaction status before executing the ping
1524 # query. If a query is already in progress (ACTIVE) or we
1525 # are inside a transaction block (INTRANS / INERROR), running
1526 # SELECT 1 would fail or disrupt the ongoing operation. In
1527 # those states the connection is evidently alive, so just
1528 # return True.
1529 # 0 = IDLE — safe to send a query
1530 # 1 = ACTIVE — command in progress, connection is alive
1531 # 2 = INTRANS — in transaction block, connection is alive
1532 # 3 = INERROR — in failed transaction, connection is alive
1533 if self.conn.info.transaction_status != 0:
1534 return True
1535
1536 cur = self.conn.cursor()
1537 cur.execute("SELECT 1")
1538 cur.close()
1539 return True
1540 except Exception:
1541 try:
1542 self.conn.close()
1543 except Exception:
1544 pass
1545 self.conn = None
1546 return False
1547
1548 def _release(self):
1549 if self.wasConnected:

Callers

nothing calls this directly

Calls 3

connectedMethod · 0.95
executeMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected