gpt4 book ai didi

java - Ping 一个 MySQL 服务器

转载 作者:IT老高 更新时间:2023-10-29 00:18:25 25 4
gpt4 key购买 nike

Java 中是否有特定的方法来检查 MySQL 服务器是否处于 Activity 状态?

最佳答案

标准 MySQL JDBC 连接器 ConnectorJ 具有轻量级 ping。来自 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.

基本上,确保您的“ping”查询以完全文本/* ping */开头。 the linked docs above中的详细信息.这让您可以利用 ping 机制,而不是执行(稍微)较重的操作。

所以基本上,做查询:

/* ping */ SELECT 1

...将触发 ping 机制,而不是实际执行 SELECT 1 的“工作”。

请注意,如果您正在谈论检查您当前未连接到的 MySQL 服务器,只需连接到该服务器即可验证它是否存在并做出响应。以上主要是关于检查 现有 连接是否仍然有效。

关于java - Ping 一个 MySQL 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4569956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com