mysql的一个坑

错误代码

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

然后就观察吧

转载于:https://my.oschina.net/wfire/blog/532477

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值