首页 > 代码库 > c3p0数据库连接池死锁问题
c3p0数据库连接池死锁问题
项目进行压力测试的时候,运行大概1小时候,后台抛出以下异常:
Java代码
- Nov 9, 2012 1:41:59 AM com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
- WARNING: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@4b9cafa7 -- APPARENT DEADLOCK!!! Complete Status:
- Managed Threads: 3
- Active Threads: 3
- Active Tasks:
- com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@19f67d43 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2)
- com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@599c706 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1)
- com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@32d95dc9 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0)
- Pending Tasks:
- com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask@5c9c5133
- com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask@1a971ac5
- com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@365e8926
- com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@787e39ac
- Pool thread stack traces:
- Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,main]
- java.lang.StringCoding.set(StringCoding.java:53)
- java.lang.StringCoding.encode(StringCoding.java:270)
- java.lang.String.getBytes(String.java:946)
- com.mysql.jdbc.ConnectionImpl.configureCharsetProperties(ConnectionImpl.java:1578)
- com.mysql.jdbc.ConnectionImpl.configureClientCharacterSet(ConnectionImpl.java:1628)
- com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3425)
- com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2045)
- com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
- com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
- sun.reflect.GeneratedConstructorAccessor35.newInstance(Unknown Source)
- sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
- java.lang.reflect.Constructor.newInstance(Constructor.java:513)
- com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
- com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
- com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
- com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:135)
- com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
- com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
- com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
- com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
- com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
- com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
- com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
- Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1,5,main]
- com.mysql.jdbc.ResultSetImpl.getInstance(ResultSetImpl.java:344)
- com.mysql.jdbc.MysqlIO.buildResultSetWithUpdates(MysqlIO.java:2607)
- com.mysql.jdbc.MysqlIO.readResultsForQueryOrUpdate(MysqlIO.java:2490)
- com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:1746)
- com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2135)
- com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)
- com.mysql.jdbc.ConnectionImpl.setupServerForTruncationChecks(ConnectionImpl.java:5240)
- com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3508)
- com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2045)
- com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
- com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
- sun.reflect.GeneratedConstructorAccessor35.newInstance(Unknown Source)
- sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
- java.lang.reflect.Constructor.newInstance(Constructor.java:513)
- com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
- com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
- com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
- com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:135)
- com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
- com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
- com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
- com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
- com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
- com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
- com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
- Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0,5,main]
- com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:562)
目测该问题是c3p0连接池导致的线程死锁。
这种数据库连接池线程死锁的问题发生的原因可能有很多,我将我的配置环境以及解决方法贴出来供大家参考一下:
使用环境,spring + hibernate +c3p0
配置如下:<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
Java代码
- destroy-method="close">
- <property name="driverClass" value=http://www.mamicode.com/"com.mysql.jdbc.Driver"/>
- <property name="jdbcUrl" value=http://www.mamicode.com/"${db.url}?useUnicode=true&characterEncoding=UTF-8"/>
- <property name="user" value=http://www.mamicode.com/"${db.usrname}"/>
- <property name="password" value=http://www.mamicode.com/"${db.password}"/>
- <!--连接池中保留的最小连接数。-->
- <property name="minPoolSize" value=http://www.mamicode.com/"5"/>
- <!--连接池中保留的最大连接数。Default: 15 -->
- <property name="maxPoolSize" value=http://www.mamicode.com/"500"/>
- <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
- <property name="initialPoolSize" value=http://www.mamicode.com/"10"/>
- <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
- <property name="maxIdleTime" value=http://www.mamicode.com/"60"/>
- <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
- <property name="acquireIncrement" value=http://www.mamicode.com/"5"/>
- <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
- 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
- 如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
- <property name="maxStatements" value=http://www.mamicode.com/"0"/>
- <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
- <property name="idleConnectionTestPeriod" value=http://www.mamicode.com/"60"/>
- <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
- <property name="acquireRetryAttempts" value=http://www.mamicode.com/"30"/>
- <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效
- 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
- 获取连接失败后该数据源将申明已断开并永久关闭。Default: false-->
- <property name="breakAfterAcquireFailure" value=http://www.mamicode.com/"false"/>
- <!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的
- 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable
- 等方法来提升连接测试的性能。Default: false -->
- <property name="testConnectionOnCheckout" value=http://www.mamicode.com/"false"/>
- </bean>
这个配置中有一些属性可能和死锁有关:maxStatements 和checkoutTimeout(我没有使用该属性)
有人说c3p0本身没有问题,导致问题是spring的原因,c3p0作为hibernate官方推荐的连接池,在性能和稳定性上都是中规中矩的。根据以前的使用经验,我判断也不是c3p0的问题。
Java代码
- <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
- 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
- 如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
- <property name="maxStatements" value=http://www.mamicode.com/"0"/>
- <!--连接池用完时客户调用getConnection()后等待获取连接的时间,单位:毫秒。超时后会抛出-->
- <!--SQLEXCEPTION,如果设置0,则无限等待。Default:0-->
- <property name="checkoutTimeout" value=http://www.mamicode.com/"100"/>
修改后,问题没有重现。
来自:http://www.cnblogs.com/zhishan/archive/2012/11/09/2761980.html
c3p0数据库连接池死锁问题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。