您的位置:首页 > 运维架构 > Tomcat

【Tomcat】tomcat报连接超时错误

2016-03-18 09:40 627 查看
程序一直报这个错误

[getui-server][ERROR] [2016-03-17 10:50:00] getui.task.HftMongoInfoTask.execute(137) | --HftMongoInfoTask-error-org.springframework.dao.RecoverableDataAccessException: PreparedStatementCallback; SQL [select mid from test_info where cid = 9]; Communications link failure

The last packet successfully received from the server was 250,825 milliseconds ago.  The last packet sent successfully to the server was 0 milliseconds ago.; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 250,825 milliseconds ago.  The last packet sent successfully to the server was 0 milliseconds ago.


百度搜了很多,很多人都遇到过这个错误,大部分都是使用连接池方式时才会出现这个问题,短连接应该很难出现这个问题。这个问题的原因:

MySQL服务器默认的“wait_timeout”是28800秒即8小时,意味着如果一个连接的空闲时间超过8个小时,MySQL将自动断开该连接,而连接池却认为该连接还是有效的(因为并未校验连接的有效性),当应用申请使用该连接时,就会导致上面的报错。

1.按照错误的提示,可以在JDBC URL中使用autoReconnect属性,实际测试时使用了autoReconnect=true&failOverReadOnly=false,不过并未起作用,使用的是5.1版本,可能真像网上所说的只对4之前的版本有效。

2.没办法,只能修改MySQL的参数了,wait_timeout最大为31536000即1年,在my.cnf中加入:

[mysqld]

wait_timeout=31536000

interactive_timeout=31536000

3.以上方法试过了,都不行。

结果是我的连接池问题,dbcp链接数据库超时,增加一个自定检测释放,就可以了。

下面是两个我解决问题的链接,希望对遇到这个问题的伙伴有帮助。


http://elf8848.iteye.com/blog/1931778

http://blog.csdn.net/njslh/article/details/9770477
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: