首先,我知道这个问题被问了很多次,但对我来说,复制场景有点不同,我猜(也可能是)。
对于 mysql 健康检查,我们执行一个简单的选择查询(从 Dual 中选择 1)。 一旦 mysql 服务器空闲一段时间(大约 1 分钟)并且我们尝试运行状况检查,我们就会遇到这个问题..
Communications link failure\n\nLast packet sent to the server was 4 ms ago
"message": "Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.", "stack": ["com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2431)", "com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2882)", "com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2871)", "com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3414)", "com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)", "com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)", "com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)", "com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465)", "com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:734)", "org.apache.commons.dbcp.DelegatingStatement.execute(DelegatingStatement.java:264)",
如果我们再次尝试,我们会立即成功连接并且一切正常。所以它本质上非常零星。
我看过这个 stackoverflow 帖子 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
所以在所有这些可能性中..我会回答其中的一些-
JDBC URL 中的 IP 地址或主机名错误。 -NO(通信失败后第二次尝试即可工作)
本地 DNS 服务器无法识别 JDBC URL 中的主机名。 -否(与步骤 1 的原因相同)
JDBC URL 中的端口号缺失或错误。 -否(与步骤 1 的原因相同)
数据库服务器已关闭。 -否-(服务器已启动并正在运行)
数据库服务器不接受 TCP/IP 连接。 -否(与步骤 1 的原因相同)
数据库服务器已耗尽连接。 -否(我已将连接数增加到 1000)
Java 和 DB 之间的某些东西正在阻塞连接,例如防火墙或代理。
是的。代理配置如下:
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
我的 etc/my.conf 的一些详细信息如下--
connect_timeout = 10
wait_timeout = 28800
bind-address = 0.0.0.0
max_connections = 1000
为了创建连接,我使用commons dbcp数据源(org.apache.commons.dbcp.BasicDataSource)
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("url");
dataSource.setUsername("username");
dataSource.setPassword("pwd");
dataSource.getConnection();
请帮我找出可能出现问题的地方..是代理配置导致了问题还是其他原因???我们将非常感谢您的帮助。
最佳答案
- IP address or hostname in JDBC URL is wrong. -NO(it works on second attempt after communication failure)
不,因为您已经到达 executeStatement()
,这意味着您已经获得了连接和 PreparedStatement
。
- Hostname in JDBC URL is not recognized by local DNS server. -No (same reason as step 1)
正确。
- Port number is missing or wrong in JDBC URL. -No (same reason as step 1)
正确。
- DB server is down. -NO-(server is up and running)
否,原因与 (1)、(2)、(3) 相同。
- DB server doesn't accept TCP/IP connections. -No (same reason as step 1)
正确。
- DB server has run out of connections. -No(I have increased number of connections to 1000)
否,原因与 (1)、(2)、(3)、(4)、(5) 相同。
- Something in between Java and DB is blocking connections, e.g. a firewall or proxy.
否,原因与 (1)、(2)、(3)、(4)、(5)、(6) 相同。
建立连接后发生通信故障。这就排除了你在这里提到的一切。剩下的就是 (a) 服务器或网络中的 transient 情况,(b) 客户端的连接池特性,(c) JDBC 驱动程序提交的一些灾难性错误命令等。最有可能的是 (a)。
关于java - 通信链路故障 最后发送到服务器的数据包是在 4 毫秒前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38638921/