解决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;来设置

posted @ 2019-03-21 08:22  mollie_x  阅读(12660)  评论(0编辑  收藏  举报