Communications link failure,The last packet successfully received from the server was *** millisecon

使用Connector/J连接MySQL数据库,程序运行较长时间后就会报以下错误:

Communications link failure,The last packet successfully received from the server was * **millisecond ago.
The last packet successfully sent to the server was * **millisecond ago。

其中错误还会提示你修改wait_timeout或是使用Connector/J的autoReconnect属性避免该错误。

后来查了一些资料,才发现遇到这个问题的人还真不少,大部分都是使用连接池方式时才会出现这个问题,短连接应该很难出现这个问题。这个问题的原因:

  1. 按照错误的提示,可以在JDBC URL中使用autoReconnect属性,实际测试时使用了autoReconnect=true&failOverReadOnly=false,不过并未起作用,使用的是5.1版本,可能真像网上所说的只对4之前的版本有效。

  2. 没办法,只能修改MySQL的参数了,wait_timeout最大为31536000即1年,在my.cnf中加入:

[mysqld]  

wait_timeout=31536000  

interactive_timeout=31536000  
  • 1

重启生效,需要同时修改这两个参数。


Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure  
[09:49:36.821]  
[09:49:36.821]Last packet sent to the server was 0 ms ago.  

猜你喜欢

转载自blog.csdn.net/suyujiezhang/article/details/76146652
今日推荐