您好,欢迎访问一九零五行业门户网

Tomcat连接池MySQL数据库8小时连接超时问题解决_MySQL

tomcat
一、问题现象:
  连接池中的链接空闲超过一定时间后,再次访问数据库时候出现如下异常:
 com.mysql.jdbc.communication***ception: communications link failure due to underlying exception:
  ** begin nested exception **
 java.net.socketexception
 message: software caused connection abort: recv failed
 ......................................................
  ** end nested exception **
 last packet sent to the server was 62 ms ago.
 mysql wait_timeout默认值为28800秒,即为8小时。也就是说默认情况下,mysql在经过8小时(28800秒)不使用后会自动关闭已打开的连接。
二、解决方法
因此可以修改mysql的wait_timeout解决问题,但是在不影响系统性能的情况下具体配置成多大不好确定。
因此考虑从tomcat连接池想法解决问题 
在mysql5以前可以在链接字符串后面增加autoreconnect=true来解决该问题。
mysql5以后autoreconnect=true已经不在起作用。
查询dbcp资料发现如下配置项目
validationquery
在连接返回给调用者前用于校验连接是否有效的sql语句。如果指定了sql语句,则必须为一个“select”语句,且至少会返回一行结果。 
因此在tomcat连接池中增加如下配置问题解决:
    testwhileidle='true' 
    timebetweenevictionrunsmillis='8000'
    minevictableidletimemillis='10000' />
其它类似信息

推荐信息