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

版权声明:本文为博主原创文章,转载请注明出处,谢谢 https://blog.csdn.net/LLittleF/article/details/78690788

最近项目部署到服务端后,一开始一切正常,第二天起床再看就没法用了。查询了一下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;

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

猜你喜欢

转载自blog.csdn.net/LLittleF/article/details/78690788