c3p0数据库连接池死锁解决

项目进行压力测试的时候,运行大概1小时候,后台抛出以下异常:

 

Java代码   收藏代码
  1. Nov 92012 1:41:59 AM com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run  
  2. WARNING: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@4b9cafa7 -- APPARENT DEADLOCK!!! Complete Status:   
  3.         Managed Threads: 3  
  4.         Active Threads: 3  
  5.         Active Tasks:   
  6.                 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@19f67d43 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2)  
  7.                 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@599c706 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1)  
  8.                 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@32d95dc9 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0)  
  9.         Pending Tasks:   
  10.                 com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask@5c9c5133  
  11.                 com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask@1a971ac5  
  12.                 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@365e8926  
  13.                 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@787e39ac  
  14. Pool thread stack traces:  
  15.         Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,main]  
  16.                 java.lang.StringCoding.set(StringCoding.java:53)  
  17.                 java.lang.StringCoding.encode(StringCoding.java:270)  
  18.                 java.lang.String.getBytes(String.java:946)  
  19.                 com.mysql.jdbc.ConnectionImpl.configureCharsetProperties(ConnectionImpl.java:1578)  
  20.                 com.mysql.jdbc.ConnectionImpl.configureClientCharacterSet(ConnectionImpl.java:1628)  
  21.                 com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3425)  
  22.                 com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2045)  
  23.                 com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:718)  
  24.                 com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:46)  
  25.                 sun.reflect.GeneratedConstructorAccessor35.newInstance(Unknown Source)  
  26.                 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)  
  27.                 java.lang.reflect.Constructor.newInstance(Constructor.java:513)  
  28.                 com.mysql.jdbc.Util.handleNewInstance(Util.java:406)  
  29.                 com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)  
  30.                 com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)  
  31.                 com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:135)  
  32.                 com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)  
  33.                 com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)  
  34.                 com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)  
  35.                 com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)  
  36.                 com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)  
  37.                 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)  
  38.                 com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)  
  39.         Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1,5,main]  
  40.                 com.mysql.jdbc.ResultSetImpl.getInstance(ResultSetImpl.java:344)  
  41.                 com.mysql.jdbc.MysqlIO.buildResultSetWithUpdates(MysqlIO.java:2607)  
  42.                 com.mysql.jdbc.MysqlIO.readResultsForQueryOrUpdate(MysqlIO.java:2490)  
  43.                 com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:1746)  
  44.                 com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2135)  
  45.                 com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)  
  46.                 com.mysql.jdbc.ConnectionImpl.setupServerForTruncationChecks(ConnectionImpl.java:5240)  
  47.                 com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3508)  
  48.                 com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2045)  
  49.                 com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:718)  
  50.                 com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:46)  
  51.                 sun.reflect.GeneratedConstructorAccessor35.newInstance(Unknown Source)  
  52.                 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)  
  53.                 java.lang.reflect.Constructor.newInstance(Constructor.java:513)  
  54.                 com.mysql.jdbc.Util.handleNewInstance(Util.java:406)  
  55.                 com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)  
  56.                 com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)  
  57.                 com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:135)  
  58.                 com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)  
  59.                 com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)  
  60.                 com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)  
  61.                 com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)  
  62.                 com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)  
  63.                 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)  
  64.                 com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)  
  65.         Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0,5,main]  
  66.                 com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:562)  

 目测该问题是c3p0连接池导致的线程死锁。

这种数据库连接池线程死锁的问题发生的原因可能有很多,我将我的配置环境以及解决方法贴出来供大家参考一下:

使用环境,spring + hibernate +c3p0

配置如下:

Java代码   收藏代码
  1.     destroy-method="close">  
  2.   "driverClass" value="com.mysql.jdbc.Driver"/>  
  3.   "jdbcUrl" value="${db.url}?useUnicode=true&characterEncoding=UTF-8"/>  
  4.           "user" value="${db.usrname}"/>  
  5.           "password" value="${db.password}"/>  
  6.     
  7.   "minPoolSize" value="5"/>  
  8.     
  9.   "maxPoolSize" value="500"/>  
  10.     
  11.   "initialPoolSize" value="10"/>  
  12.     
  13.   "maxIdleTime" value="60"/>  
  14.     
  15.   "acquireIncrement" value="5"/>  
  16.     
  17.   "maxStatements" value="0"/>  
  18.     
  19.   "idleConnectionTestPeriod" value="60"/>  
  20.     
  21.   "acquireRetryAttempts" value="30"/>  
  22.     
  23.   "breakAfterAcquireFailure" value="false"/>  
  24.     
  25.   "testConnectionOnCheckout" value="false"/>  
  26.       

 这个配置中有一些属性可能和死锁有关:maxStatements 和checkoutTimeout(我没有使用该属性)

有人说c3p0本身没有问题,导致问题是spring的原因,c3p0作为hibernate官方推荐的连接池,在性能和稳定性上都是中规中矩的。根据以前的使用经验,我判断也不是c3p0的问题。

Java代码   收藏代码
  1.   
  2.  "maxStatements" value="0"/>  
  3.   
  4.    
  5.  "checkoutTimeout" value="100"/>  
 

  修改后,问题没有重现。

网站建设 管理系统开发 微信小程序开发

你可能感兴趣的:(C3P0)