Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

java - Ping a MySQL server

Is there a specific method in Java that checks whether a MySQL server is alive?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...