root cause:
The last packet sent successfully to the server was 78,340,024 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经典的8小时问题!
修改了两个属性 一个是maxWait 和 url
本文参照前辈文章 :
1、这篇文章提供了两种目前最实用的解决方案:https://blog.csdn.net/pandajava/article/details/41946251
2、这篇文章详细配置了连接池:https://blog.csdn.net/opensure/article/details/46124421
3、这篇文章介绍了使用BasicDataSource引发的数据库连接中断的问题和解决方法:https://blog.csdn.net/yinyuehepijiu/article/details/31353039
以上方法中经过测试,只有修改URL的方法确实不适用!
(本段援引自:https://bbs.csdn.net/wap/topics/260037687)
DBCP是用来配置连接池的需要的包:
Commons-Pool (下载地址:http://jakarta.apache.org/commons/pool/) :提供一个一般对象池的接口,而且包含实现了通常池工具箱。
Commons-DBCP(下载地址:http://jakarta.apache.org/commons/dbcp/) :提供数据库连接池服务。DBCP很聪明,把Commons-Pool和JDBC的driver封装起来,使达到连接池的目的。
方法一:
下图 是第二篇文章中的通过配置连接池的属性来解决mysql中断的问题:
下图 是我参照第三篇文章通过配置DBCP的属性来解决mysql中断的问题:
第三篇文章介绍此部分很详细。
//是否要进行检测
<property name="testWhileIdle" value="true"/>
//进行检测一个连接是有效的SQL语句,比如oracle是select 1 from dual 而 mysql是 select 1
<property name="validationQuery" value=""/>
//是否在数据库连接请求量大的时候,如总数50,当前已请求了49个,所剩不多了,检测那些执行时间久的连接,将其从池中移除掉(移除之后怎么作,由实现决定,如直接断开,或者任其继续执行等)
<property name="removeAbandoned" value="true"/>
//一次数据库操作执行时间超过多少秒的连接被认为是需要移除的
<property name="removeAbandonedTimeout" value=""/>
//每隔多少时间检测一次,比如每半小时检测一次,总不能总是检测,这会对性能产生影响
<property name="timeBetweenEvictionRunsMillis" value=""/>
//每次检测时,需要检测多少个数据连接,一般设置为与最大连接数一样,这样就可以检测完所有的连接
<property name="numTestsPerEvictionRun" value=""/>
//一个数据库连接连接多少时间之外,我们认为其应该不再适用了(可能下一次就会失效了),应该移除并重新建立连接了
<property name="minEvictableIdleTimeMillis" value=""/>
挑选三个属性进行配置 :
直接对连接池配置:
<property name="poolPingEnabled" value="true"/>
<property name="poolPingQuery" value="select now() from kpi.lastupdatedlog limit 1"/>
<property name="poolPingConnectionsNotUsedFor" value="3600000"/>
对DBCP进行配置 :
//是否要进行检测
<property name="testWhileIdle" value="true"/>
//进行检测一个连接是有效的SQL语句,比如oracle是select 1 from dual 而 mysql是 select 1
<property name="validationQuery" value="select 1"/>
//每隔多少时间检测一次,比如每半小时检测一次,总不能总是检测,这会对性能产生影响
<property name="timeBetweenEvictionRunsMillis" value="3600000"/>
方法二:
increasing the server configured values for client timeouts 为用户增加服务器连接超时的时间。 mysql允许的最大闲置时间,默认是8小时,最大是1年。而这种方法就是将连接池中的连接可能闲置时间尽可能的往大设置。 显然这不是一个好方法。原因如下:
1、闲置时间是有上限的,在极端情况下,还是可能发生异常。
2、长时间保留闲置的连接,会降低数据库性能,消耗内存,最终耗尽数据库的连接数。所以不采用增大闲置时间方法。
大致方法如下:
通过修改MySQL的参数了,将wait_timeout最大为31536000即1年
在my.cnf中加入:
[mysqld]
wait_timeout=31536000
interactive_timeout=31536000
重启生效,需要同时修改这两个参数。