java 使用 JDBC ping MySQL 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5445917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:10:19  来源:igfitidea点击:

Ping MySQL Server Using JDBC

javamysqljdbcping

提问by Brian Roach

This seems rather simple (not for me)...

这似乎很简单(不适合我)...

How does one ping a mysql server using the JDBC? i have successfully used select and insert queries with JDBC and MySQL, but how to ping???

如何使用 JDBC ping mysql 服务器?我已经成功地在 JDBC 和 MySQL 中使用了选择和插入查询,但是如何 ping ???

TIA

TIA

回答by Brian Roach

The MySQL JDBC driver (Connector/J) provides a ping mechanism.

MySQL JDBC 驱动程序 (Connector/J) 提供了一种 ping 机制。

If you do a SQL query prepended with /* ping */such as:

如果您执行 SQL 查询,/* ping */例如:

"/* ping */ SELECT 1"

This will actually cause the driver send a ping to the server and return a fake, light-weight, result set.

这实际上会导致驱动程序向服务器发送一个 ping 并返回一个假的、轻量级的结果集。

(You can find this buried fairly deep in the Connector/J documentation; search for "ping" on that page. Read it carefully: this mechanism is very sensitiveto the syntax used. Unlike most SQL, the "parsing" of the "ping" marker happens in the client-side JDBC driver itself.).

(您可以在Connector/J 文档中找到这个隐藏得很深的内容;在该页面上搜索“ping”。仔细阅读:此机制对所使用的语法非常敏感。与大多数 SQL 不同,“ping”的“解析” " 标记发生在客户端 JDBC 驱动程序本身中。)。

回答by a_horse_with_no_name

I don't think there is anything in the JDBC APi for this.

我认为 JDBC APi 中没有任何用于此的内容。

But you can simply use InetAddress and the isReachable() method to check if the server is there (which is essentially what a ping is doing).

但是您可以简单地使用 InetAddress 和 isReachable() 方法来检查服务器是否在那里(这本质上是 ping 正在执行的操作)。

http://download.oracle.com/javase/6/docs/api/java/net/InetAddress.html#isReachable%28int%29

http://download.oracle.com/javase/6/docs/api/java/net/InetAddress.html#isReachable%28int%29

Edit:
If you want to check if MySQL is running rather than pinging the server you could try to open a socket to the server on the MySQL default port.

编辑
如果您想检查 MySQL 是否正在运行而不是 ping 服务器,您可以尝试在 MySQL 默认端口上打开一个到服务器的套接字。

Something like this:

像这样的东西:

InetAddress addr = InetAddress.getByName("your.server.com");
int port = 3306;
SocketAddress sockaddr = new InetSocketAddress(addr, port);
Socket sock = new Socket();
sock.connect(sockaddr, 2000); // open the connection with a 2 seconds timeout

If connect() does not throw an exception something is running on port 3306.

如果 connect() 没有抛出异常,则端口 3306 上正在运行某些内容。

If you want to make sure that it's a MySQL server on that port, the only way is to establish a JDBC connection (but you need a valid username/password for that) and run a statement e.g. the PING statement mentioned by Brian.

如果你想确保它是那个端口上的 MySQL 服务器,唯一的方法是建立一个 JDBC 连接(但你需要一个有效的用户名/密码)并运行一个语句,例如 Brian 提到的 PING 语句。

回答by Buhake Sindi

I don't know how to do this in JDBC by you can use Connection Pooling to validate your connection like so (This is the context.xmlfor Tomcat (found in the META-INFdirectory of your application)):

我不知道如何在 JDBC 中执行此操作,您可以使用连接池来验证您的连接(这是context.xmlTomcat 的(在META-INF您的应用程序目录中找到)):

<Resource name="jdbc/My_DS" auth="Container" type="javax.sql.DataSource"
            maxActive="100" maxIdle="30" maxWait="10000"
            username="USERNAME" password="PASSWORD" driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/MYDB?autoReconnect=true" 
            removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true"
            testOnBorrow="true" validationQuery="SELECT 1" />

The validationQueryvalidates if the connection is successful or not.

validationQuery,如果连接成功与否验证。