Issue :The last packet successfully received from the server was XXXXXX seconds ago.
The last packet sent successfully to the server was XXXXXX 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

原因,连接池超出了MySQL设置的'wait_timeout'时长啦。
查看 wait_timeout
mysql> show global variables like 'wait_timeout';

mysql默认为8小时

解决方案:1.增加MySQL 超时 时长
(1)my.cnf(windows下为my.ini),将超时时间设置为长点,网上很多是设置10年,
在MYSQLD 后面加入:wait_timeout = 315360000
(实际设置 后 为1年, 即31536000)
(2)mysql> set global wait_timeout=315360000;
重启 mysql
但该方法不被推荐,应用如果不能确定最长'wait_timeout'时间,则该值无论如何设定,理论上也无法避免如上问题。所以只能保证应用在MySQL的'wait_timeout'时间内,至少访问一次数据库。

2.代码端 select 轮循
只需配置dbcp的三个配置项timeBetweenEvictionRunsMillis、testWhileIdle与validationQuery即可。

timeBetweenEvictionRunsMillis=86400 # 失效检查线程运行时间间隔,要小于MySQL的'wait_timeout'时间(如果小于等于0,不会启动检查线程)
testWhileIdle=true # 检查连接是否有效
validationQuery=SELECT 1 FROM dual # 检查连接有效性的SQL语句

这样dbcp会在timeBetweenEvictionRunsMillis指定的时间间隔(小于MySQL的'wait_timeout')内,通过validationQuery指定的SQL语句来检查连接是否有效。避免了连接因长时间未执行SQL语句,而造成MySQL关闭连接。

实际spring中 为以下配置

<property name="timeBetweenEvictionRunsMillis">
    <value>3600000</value>
</property>
<property name="testWhileIdle">
    <value>true</value>
</property>
<property name="validationQuery">
    <value>select 1 from dual</value>
</property>

最后还要注意一点,有可能因为git协作,数据库配置可能被 小伙伴改掉哦~ 我就是这样滴

« When to use NSInteger vs. int android wear app设计理念(官网需要一个好的国际网络环境) »