错误代码
Unexpected error occurred in scheduled task.
org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection
for transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
No operations allowed after connection closed.
。。。。。。
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed
after connection closed.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
。。。。。。
org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(
DataSourceTransactionManager.
java:223)
... 22 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 59,942 milliseconds ago.
The last packet sent successfully to the server was 24 milliseconds ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
。。。。。。
org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(
DataSourceTransactionManager.
java:212)
... 22 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:2914)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3332)
原因
MySQL服务器默认的“wait_timeout”是28800秒即8小时,意味着如果一个连接的空闲时间超过8个小时,MySQL将自动断开该连接,而连接池却认为该连接还是有效的(因为并未校验连接的有效性),当应用申请使用该连接时,就会导致上面的报错。
解决办法
修改mysql的配置文件 my.cnf 这个文件一般在mysql的安装目录或者/etc/my.cnf
修改内容如下 超时时间改为1年
[mysqld]
wait_timeout=31536000
interactive_timeout=31536000
修改完保存之后,重启mysql
然后就观察吧