Communications link failure

来源:互联网 发布:新奔腾造价软件 编辑:程序博客网 时间:2024/05/23 01:21
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure解决  


2014-05-15 10:10:18|  分类: 数据库 |  标签:mysql  communications  link  failure   |举报|字号 订阅
错误日志:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure






The last packet successfully received from the server was 42,630,542 milliseconds ago.  The last packet sent successfully to the server was 0 milliseconds ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3090)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2979)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3520)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2619)
at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:4997)
at org.apache.commons.dbcp.DelegatingConnection.setAutoCommit(DelegatingConnection.java:331)
at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.setAutoCommit(PoolingDataSource.java:317)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:87)
... 89 more
Caused by: java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:114)
at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:161)
at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:189)
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2537)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2990)
... 98 more




查看数据库配置:


mysql﹥ show global variables like 'wait_timeout';


+---------------+---------+
| Variable_name | Value |
+---------------+---------+
| wait_timeout | 28800 |             //---->默认空闲连接时间8个小时,超时将关闭该连接
+---------------+---------+
1 row in set (0.00 sec)




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


解决方法:


1、修改my.cnf: 
 
[mysqld]  
wait_timeout=1728000
interactive_timeout=1728000
 
将过期时间修改为20天。 
 
2、在连接URL上添加参数:&autoReconnect=true&failOverReadOnly=false 


我使用的是第一种方式,如果不会改修改本篇日志说明相应的配置已经起作用。
参考地址:http://www.2cto.com/database/201305/211381.html








++++++++++++++++++++




“SEVERE: The last packet successfully received from the server was144382 seconds ago.The last packet sent successfully to the server was 144382 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.” 
你的log里说的很清楚了。原因是Mysql服务器默认的“wait_timeout”是8小时,如果一个connection空闲超过8个小时,Mysql将自动断开该 connection,而C3P0并不知道该connection已经失效,如果这时请求connection,将会造成上面的异常。你每次使用前判断connection是否有效就会避免这种异常。 


解决的方法有3种: 


增加wait_timeout的时间。 
减少Connection pools中connection的lifetime。 
测试Connection pools中connection的有效性。 
当然最好的办法是同时综合使用上述3种方法,下面举个例子,假设wait_timeout为默认的8小时 


C3P0增加以下配置信息: 


//获取connnection时测试是否有效 
testConnectionOnCheckin = true 
//自动测试的table名称 


automaticTestTable=C3P0TestTable 


//set to something much less than wait_timeout, prevents connections from going stale 
idleConnectionTestPeriod = 18000 
//set to something slightly less than wait_timeout, preventing 'stale' connections from being handed out 
maxIdleTime = 25000 
//if you can take the performance 'hit', set to "true" 
testConnectionOnCheckout = true
0 0