解决servlet长时间连接数据库时,数据库自动关闭的问题

12 篇文章 0 订阅
7 篇文章 0 订阅

最近项目部署到服务端后,一开始一切正常,第二天起床再看就没法用了。查询了一下tomcat的日志,发现抛出了com.mysql.jdbc.exceptions.jdbc4.CommunicationsException异常,具体内容为:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 38,031,596 milliseconds ago.  The last packet sent successfully to the server was 38,031,606 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.

大致是距离上次查询数据库已经过了38031596毫秒(大概十个小时),超过了变量wait_timeout所规定的最大值。

解决方法:最新版本的mysql貌似不支持上述的自动连接,只能通过更改wait_timeout的方法来解决。使用root用户登录mysql
先看看这个变量的值为多少:
mysql> show global variables like ‘wait_timeout’;
我的显示为 28800(秒),也就是八个小时。linux最大支持365天,所以可以这样设置:
mysql> set global wait_timeout = 31536000;

虽说这样有点治标不治本,但是一年的时间,服务器怎么也得宕几次吧(^_^)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值