Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

问题描述:

程序在运行的时候,刷新页面,会出现以下错误

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure


Last packet sent to the server was 0 ms ago.
at sun.reflect.GeneratedConstructorAccessor36.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2871)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3414)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)
at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:4874)
at com.mchange.v2.c3p0.impl.NewProxyConnection.setAutoCommit(NewProxyConnection.java:912)
at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:83)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
... 78 more
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2431)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2882)

 

... 87 more

 

上述问题是由mysql数据库的配置引起的。mysql将其连接的等待时间(wait_timeout)缺省为8小时。在其客户程序中可以这样来查看其值: 

mysql> show global variables like "wait_timeout";
+---------------+---------+
| Variable_name | Value |
+---------------+---------+
| wait_timeout | 1814400 |
+---------------+---------+
1 row in set (0.00 sec)
 

如果在wait_timeout秒期间内,数据库连接(java.sql.Connection)一直处于等待状态,mysql就将该连接关闭。这时,你的Java应用的连接池仍然合法地持有该连接的引用。当用该连接来进行数据库操作时,就碰到上述错误。

解决办法:

   1、修改MySQL数据配置文件,Windows文件名是my.ini,路径在MYSQL安装路径下;Linux中文件名是my.cnf   路径在/etc/my.cnf。修改wait_timeout = 1814400;

   2、重启MySQL数据库: service mysqld restart

解决方法补充:

    1、上述解决方法中修改了wait_timeout ,但是我的是Linux 中的MySql版本是5.6的,我试着修改了下mysql/my.cnf 中的max_connetions=1814400 ,然后测试了一下也是可以的。

转载于:https://my.oschina.net/xiaominmin/blog/2999284

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值