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

Problem description:

When the program is running and the page is refreshed, the following error occurs

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

The above problem is caused by the configuration of the mysql database. mysql defaults its connection wait_timeout to 8 hours. The value can be viewed in its client program as follows.

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

If the database connection (java.sql.Connection) remains in the wait_timeout state for the duration of the wait_timeout seconds, mysql closes the connection. At this point, your Java application’s connection pool still legally holds a reference to that connection. When using that connection to perform database operations, you encounter the above error.

Solution:

1. modify the MySQL data configuration file, the Windows filename is my.ini, the path is under the MYSQL installation path; in Linux the filename is my.cnf the path is in /etc/my.cnf. modify wait_timeout = 1814400.

2. Restart the MySQL database: service mysqld restart

Solution Supplement:

1, the above solution modified the wait_timeout , but my Linux MySql version is 5.6, I tried to modify the mysql/my.cnf max_connetions = 1814400, and then tested it is also possible.

Similar Posts: