The standard MySQL JDBC connector, ConnectorJ, has a lightweight ping. From the docs:
MySQL Connector/J has the ability to execute a lightweight ping against a server, in order to validate the connection. In the case of load-balanced connections, this is performed against all active pooled internal connections that are retained. This is beneficial to Java applications using connection pools, as the pool can use this feature to validate connections.
Basically, ensure that your "ping" query starts with exactly the text /* ping */
. Details in the linked docs above. This lets you take advantage of the pinging mechanism rather than doing a (slightly) heavier weight operation.
So basically, doing the query:
/* ping */ SELECT 1
...will trigger the ping mechanism rather than actually doing the "work" of a SELECT 1
.
Mind you, if you're talking about checking a MySQL server that you're not currently connected to, just the act of connecting to it verifies that it's there and responding. The above is mostly about checking that an existing connection is still valid.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…