mysql 报com.mysql.jdbc.exceptions.jdbc4.CommunicationsException

早上看日志发现连接超时报错:
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was48477 seconds ago.The last packet sent successfully to the server was 48477 seconds ago, which 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.

解决方法:
这是一个标准的servlet/tomcat网络应用,后台使用mysql数据库。问题是待机一晚上后,第二天早上第一次登录总是失败。mysql5将其连接的等待时间(wait_timeout)默认缺省为8小时。在客户端查看其值:
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的最大值分别是24天/365天。
修改 /etc/my.cnf 添加:
wait_timeout=1814400
需要重新启动mysql5。
或者mysql客户端
set global wait_timeout=1814400;

猜你喜欢

转载自blog.csdn.net/chenshijie2011/article/details/79030935