码迷,mamicode.com
首页 > 数据库 > 详细

MySQL异常【数据库断开连接】:Communications link failure

时间:2015-08-17 14:12:34      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

一.异常信息以及解决办法

      
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure 
org.springframework.transaction.CannotCreateTransactionException: 
    Could not open JDBC Connection for transaction; 
    nested exception iscom.mysql.jdbc.exceptions.jdbc4.CommunicationsException: 
    Communications link failure
The last packet successfully received from the server was 6,388 milliseconds ago.  
The last packet sent successfully to the server was 1,504 milliseconds ago.
at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:240)

异常分析:程序与MySQL通讯失败了,即连接失败了。

       此为程序打开数据库连接后,等到做数据库操作时,发现连接被MySQL关闭掉了。而在MySQL这一层,MySQL5配置上默认将连接的等待时间(wait_timeout)缺省为8小时。连接超过8小时,会导致mysql认为这个连接超时无效,然后进行关闭。

mysql﹥ 
mysql﹥ show global variables like ‘wait_timeout‘; 
+---------------+---------+ 
| Variable_name | Value | 
+---------------+---------+ 
| wait_timeout | 28800 | 
+---------------+---------+ 
1 row in set (0.00 sec) 
28800 seconds,也就是8小时。


解决办法(优先尝试第三种方案):
(1)在jdbc连接url的配置中,你可以附上“autoReconnect=true”,但这仅对mysql5以前的版本起作用。

(2)既然问题是由mysql5的全局变量wait_timeout的缺省值太小引起的,我们将其改大就好了。 
查看mysql5的手册,发现对wait_timeout的最大值分别是24天/365天(windows/linux)。以windows为 例,假设我们要将其设为21天,我们只要修改mysql5的配置文件“my.ini”(mysql5 installation dir),增加一行:wait_timeout=1814400 ,需要重新启动mysql5。 
linux系统配置文件:/etc/my.cnf 

(3)我们可以将数据库连接池的 validateQuery、testOnBorrow(testOnReturn)打开,这样在 每次从连接池中取出且准备使用之前(或者使用完且在放入连接池之前)先测试下当前使用是否好用,如果不好用,系统就会自动destory掉。
或者testWhileIdle项是设置是否让后台线程定时检查连接池中连接的可用性。



MySQL异常【数据库断开连接】:Communications link failure

标签:

原文地址:http://my.oschina.net/xsh1208/blog/493443

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!