关于mysql数据库连接超时的解决办法

来源:互联网 发布:棉花期货天狼星软件 编辑:程序博客网 时间:2024/04/30 02:13

异常信息如下:

The last packet successfully received from the server was 63,004,860 milliseconds ago.  The last packet sent successfully to the server was 63,004,905 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服务器成功接收的数据包是63004860毫秒之前,也就是前一天,最后从服务器发送成功的包也是前一天,它比mysql服务器配置的"wait_timeout"(最大超时时间)要长,所以就出现了该bug。

从百度到的解决方案有如下两种:

1、直接修改mysql的配置文件,增加wait_timeout,但是这个参数的最大值好像是21天,而21天也有可能出现断开的情况,显然不是一劳永逸的解决办法。

2、修改数据源的url属性,在url后面增加autoReconnect=true属性,这也是报错时,程序提示我的解决办法,但是mysql5后,好像不支持autoReconnect=true的属性了。


最后百度到了以下博文,解决了我的问题,在此对博主表示感谢:


http://blog.csdn.net/xuke6677/article/details/45146823


由于我使用的是dbcp数据源,所以,我直接参照他的解决办法,在dbcp数据源的配置中,增加以下两行配置:

<property name="minEvictableIdleTimeMillis"><value>3600000</value></property><property name="timeBetweenEvictionRunsMillis"><value>600000</value></property>

minEvictableIdleTimeMillis=3600000:超过一个小时没连接的移除连接池

timeBetweenEvictionRunsMillis=600000:每10分钟运行一次超时任务检查线程,发现超过一个小时没连接的就移除出去,这个属性必须配置,该属性

默认值是-1,不开启超时检查的线程




0 0
原创粉丝点击