解决com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 126,421,823 milliseconds ago. The last packet sent successfully to the server was 126,421,823 milliseconds ago. is longer than the server configured value of ‘wait_timeout’. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property ‘autoReconnect=true’ to avoid this problem.

 

解决办法:

修改mysql配置文件,更改连接失效时间

mysql﹥

mysql﹥ show global variables like ‘wait_timeout’;

+—————+———+

| Variable_name | Value |

+—————+———+

| wait_timeout | 28800 |

+—————+———+

1 row in set (0.00 sec)

28800 seconds,也就是8小时。

如果在wait_timeout秒期间内,数据库连接(java.sql.Connection)一直处于等待状态,mysql5就将该连接关闭。这时,你的Java应用的连接池仍然合法地持有该连接的引用。当用该连接来进行数据库操作时,就碰到上述错误。这解释了为什么我的程序第二天不能登录 的问题。

你可能会想到在tomcat的数据源配置中有没有办法解决?的确,在jdbc连接url的配置中,你可以附上“autoReconnect=true”,但这仅对mysql5以前的版本起作用。增加“validation query”似乎也无济于事。

本人觉得最简单的办法,就是对症下药:既然问题是由mysql5的全局变量wait_timeout的缺省值太小引起的,我们将其改大就好了。

查看mysql5的手册,发现对wait_timeout的最大值分别是24天/365天(windows/linux)。以windows为 例,假设我们要将其设为21天,我们只要修改mysql5的配置文件“my.ini”(mysql5 installation dir),增加一行:wait_timeout=1814400

需要重新启动mysql5。

linux系统配置文件:/etc/my.cnf

测试显示问题解决了。

也可以直接设置

mysql修改wait_timeout

  mysql mysql> show global variables like ‘wait_timeout’;

  其默认值为8小时

  mysql的一个connection空闲时间超过8小时,mysql会自动断开该连接。

  1.修改配置

  vi /etc/my.cnf

  [mysqld] wait_timeout=10

  # /etc/init.d/mysql restart

  2.直接用sql命令行修改 mysql> set global wait_timeout=604800;

除了wait_timeout,还有一个’interactive_timeout’

同样可以执行SHOW GLOBAL VARIABLES LIKE ‘interactive_timeout’;来查询
执行set global interactive_timeout=604800;来设置

转载于:https://www.cnblogs.com/mollie-x/p/10569301.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值