> 数据库 > MySQL >

解决“连接数据库是老是报Communications link failure due to underlying exception”

参考:http://www.blogjava.net/ashutc/archive/2011/03/16/346365.html

 

查看了Mysql的文档,以及Connector/J的文档以及在线说明发现,出现这种异常的原因是:

Mysql服务器默认的“wait_timeout”是8小时,也就是说一个connection空闲超过8个小时,Mysql将自动断开该 connection。这就是问题的所在,在C3P0 pools中的connections如果空闲超过8小时,Mysql将其断开,而C3P0并不知道该connection已经失效,如果这时有 Client请求connection,C3P0将该失效的Connection提供给Client,将会造成上面的异常。

解决的方法有3种:

  1. 增加wait_timeout的时间。
  2. 减少Connection pools中connection的lifetime。
  3. 测试Connection pools中connection的有效性。

当然最好的办法是同时综合使用上述3种方法,下面就DBCP和C3P0分别做一说明,假设wait_timeout为默认的8小时

DBCP增加以下配置信息:

  1. //set to 'SELECT 1'
  2. validationQuery = "SELECT 1"
  3. //set to 'true'
  4. testWhileIdle = "true"
  5. //some positive integer
  6. timeBetweenEvictionRunsMillis = 3600000
  7. //set to something smaller than 'wait_timeout'
  8. minEvictableIdleTimeMillis = 18000000
  9. //if you don't mind a hit for every getConnection(), set to "true"
  10. testOnBorrow = "true"

C3P0增加以下配置信息:

  1. //获取connnection时测试是否有效
  2. testConnectionOnCheckin = true
  3. //自动测试的table名称
  4. automaticTestTable=C3P0TestTable
  5. //set to something much less than wait_timeout, prevents connections from going stale
  6. idleConnectionTestPeriod = 18000
  7. //set to something slightly less than wait_timeout, preventing 'stale' connections from being handed out
  8. maxIdleTime = 25000
  9. //if you can take the performance 'hit', set to "true"
  10. testConnectionOnCheckout = true

更多的配置信息大家可以查看C3P0文档,Connector/J文档,以及DBCP的文档。

转: http://www.javaeye.com/article/38506

我自己的配置:

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl = jdbc:mysql://localhost:3306/test
jdbc.user = root
jdbc.password = 12345
jdbc.miniPoolSize = 1
jdbc.maxPoolSize = 20
jdbc.initialPoolSize = 1
jdbc.maxIdleTime = 25000
jdbc.acquireIncrement = 1

jdbc.acquireRetryAttempts = 30
jdbc.acquireRetryDelay = 1000
jdbc.testConnectionOnCheckin = true
#jdbc.automaticTestTable = c3p0TestTable
jdbc.idleConnectionTestPeriod = 18000
jdbc.checkoutTimeout=3000

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
   <property name="driverClass" value="${jdbc.driverClass}" />
   <property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
   <property name="user" value="${jdbc.user}" />
   <property name="password" value="${jdbc.password}" />
   <property name="minPoolSize" value="${jdbc.miniPoolSize}" />
   <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>  
   <property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>
   <property name="maxIdleTime" value="${jdbc.maxIdleTime}"/>
   <property name="acquireIncrement" value="${jdbc.acquireIncrement}"/>
  
   <property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}"/>
   <property name="acquireRetryDelay" value="${jdbc.acquireRetryDelay}"/>
   <property name="testConnectionOnCheckin" value="${jdbc.testConnectionOnCheckin}"/>
   <property name="automaticTestTable" value="${jdbc.automaticTestTable}"/>
   <property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}"/>
   <property name="checkoutTimeout" value="${jdbc.checkoutTimeout}"/>

</bean>

+++++++++++

报错误: 
APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks! 

一般设置maxStatements=0解决该问题 
但是: 
把max_statements设置为0。 
c3p0在同时关闭statement和connection的时候,或者关闭他们之间的时间很短的时候,有时候connection并没有被关闭,因为有些preparedstatement还在被cached住。这是c3p0的作者自己说的。 
http://forum.hibernate.org/viewtopic.php?t=947246&highlight=apparent+deadlock+c3p0 

C3P0增加以下配置信息: 


//set to 'SELECT 1'       
preferredTestQuery = 'SELECT 1'     
//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     



A c3p0 pool with the settings you have should recover from a database reset, but that doesn't mean you will never see an Exception. Stale Connections from the old database session will still be broken, and if those Connections have already been checked out, or if they are in the pool and not tested on checkout, the application will see the broken Connection, in the form of an Exception. 

You can use c3p0 to minimize the likelihood that your application will see a stale Connection on database shutdown/restart. The most reliable means of preventing this is to set hibernate.c3p0.validate to true (in a hibernate application -- all other c3p0 apps should use the c3p0-native property c3p0.testConnectionOnCheckout). If you set this property to true, c3p0 will test Connections prior to checkout, and your app will never see a stale Connection on database restart unless the Connection had already been checked out when the database went down. 

Another less reliable, but potentially less expensive, strategy is to set c3p0.testConnectionsOnCheckin and hibernate.c3p0.idle_test_period (c3p0-native c3p0.idleConnectionTestPeriod) to a low value, in which case all connection tests are asynchronous and you are guanteed that no Connection will be checked out that hasn't been tested in the last idle_test_period seconds. Thus, your app will only see broken Connections from the pool if Connections are checked out during a short window of time. 

In either case, I recommend setting "c3p0.preferredTestQuery " or "c3p0.automaticTestTable" in your c3p0 properties file, as c3p0's default Connection test is often slow. 

See "Configuring Connection Testing" in c3p0's docs for more information. 



在 使用c3p0作为连接池时,其中的一些配置参数需要修改。主要是maxIdleTime和idleConnectionTestPeriod。 MySQL默认是8小时(28800秒)后自动关闭已打开的连接,所以c3p0要在8小时内关闭不使用的连接,上面的2参数要小于28800秒。附上在 hibernate中配置c3p0的关键字。 

c3p0-native property name hibernate configuration key 
c3p0.acquireIncrement hibernate.c3p0.acquire_increment 
c3p0.idleConnectionTestPeriod hibernate.c3p0.idle_test_period 
c3p0.initialPoolSize not available -- uses minimum size 
c3p0.maxIdleTime hibernate.c3p0.timeout 
c3p0.maxPoolSize hibernate.c3p0.max_size 
c3p0.maxStatements hibernate.c3p0.max_statements 
c3p0.minPoolSize hibernate.c3p0.min_size


 

(责任编辑:IT)