Home > Database > Mysql Tutorial > Tomcat连接池MySQL数据库8小时连接超时问题解决_MySQL

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

WBOY
Release: 2016-05-31 08:49:01
Original
2013 people have browsed it

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' /> 


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template