问题描述:
程序在运行的时候,刷新页面,会出现以下错误
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 ,然后测试了一下也是可以的。