c3p0-APPARENT DEADLOCK

来源:互联网 发布:前端网页优化学什么 编辑:程序博客网 时间:2024/05/16 19:55

http://japi.javaeye.com/blog/243702

 

2008-09-18

C3P0使用出错!

文章分类:Java编程
网站今日又出现不能访问的原因:
日志如下:

Java代码 复制代码
  1. [framework] 2008-09-18 18:36:05,109 - com.mchange.v2.async.ThreadPoolAsynchronousRunner -46651078 [Timer-18] WARN  com.mchange.v2.async.ThreadPoolAsynchronousRunner  - com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@1c17bd4 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!  


网上说是连接池的原因....

我的C3P0配置
Java代码 复制代码
  1. <property name="hibernate.connection.provider_class">   
  2.         org.hibernate.connection.C3P0ConnectionProvider   
  3.     </property>   
  4.     <property name="c3p0.min_size">20</property>   
  5.     <property name="c3p0.max_size">200</property>   
  6.     <property name="c3p0.time_out">1800</property>   
  7.     <property name="c3p0.max_statement">100</property>   
  8.     <property name="c3p0.validate">true</property>  


对于以上错误,查阅了类似问题网友的解答:
一:http://www.javaeye.com/topic/22160?page=3
引用
把max_statements设置为0。
c3p0在同时关闭statement和connection的时候,或者关闭他们之间的时间很短的时候,有时候connection并没有被关闭,因为有些preparedstatement还在被cached住。这是c3p0的作者自己说的。
http://forum.hibernate.org/viewtopic.php?t=947246&highlight=apparent+deadlock+c3p0

对于这个解答,看下我的设置:
Java代码 复制代码
  1. <property name="c3p0.max_statement">100</property>  

因为这里c3p0.max_statements写错了,所以设置无效,看下日志
Java代码 复制代码
  1. [framework] 2008-09-18 06:32:48,906 - com.mchange.v2.c3p0.C3P0Registry -3257219 [TP-Processor1] INFO  com.mchange.v2.c3p0.C3P0Registry  - Initializing c3p0-0.9.1 [built 16-January-2007 14:46:42; debug? true; trace: 10]   
  2.  [framework] 2008-09-18 06:32:48,906 - com.mchange.v2.c3p0.management.ActiveManagementCoordinator -3257219 [TP-Processor1] WARN  com.mchange.v2.c3p0.management.ActiveManagementCoordinator  - A C3P0Registry mbean is already registered. This probably means that an application using c3p0 was undeployed, but not all PooledDataSources were closed prior to undeployment. This may lead to resource leaks over time. Please take care to close all PooledDataSources.   
  3.  [framework] 2008-09-18 06:32:49,015 - com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource -3257328 [TP-Processor1] INFO  com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource  - Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@7111383 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@a9b46d3 [ acquireIncrement -> 1, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 2rysjs7wvr5h721dikp1v|1880048, idleConnectionTestPeriod -> 0, initialPoolSize -> 20, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 200, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 20, nestedDataSource ->   

maxStatements的默认值为0
所以这种解决方法,感觉不行

二.http://yonguo.javaeye.com/blog/120574
引用
更改配置文件里的最大连接数和初始化连接数大小:
Xml代码 复制代码
  1. <property name="maxPoolSize" value="1"/>      
  2. <property name="initialPoolSize" value="1"/>    


因为数据库的连接数是有限的,每次应用启动C3p0都会占用数据库的连接来填充C3p0的连接池,而当数据库的资源被占光时就会因为无法获得共享资源而报死锁。

此方法可能有理,但没有测试.

三.http://www.javaeye.com/problems/1082
引用
Basic Pool Configuration 
c3p0 Connection pools are very easy to configure via the following basic parameters:

acquireIncrement
initialPoolSize
maxPoolSize
maxIdleTime
minPoolSize
initialPoolSize, minPoolSize, maxPoolSize define the number of Connections that will be pooled. Please ensure that minPoolSize <= maxPoolSize. Unreasonable values of initialPoolSize will be ignored, and minPoolSize will be used instead.

Within the range between minPoolSize and maxPoolSize, the number of Connections in a pool varies according to usage patterns. The number of Connections increases whenever a Connection is requested by a user, no Connections are available, and the pool has not yet reached maxPoolSize in the number of Connections managed. Since Connection acquisition is very slow, it is almost always useful to increase the number of Connections eagerly, in batches, rather than forcing each client to wait for a new Connection to provoke a single acquisition when the load is increasing. acquireIncrement determines how many Connections a c3p0 pool will attempt to acquire when the pool has run out of Connections. (Regardless of acquireIncrement, the pool will never allow maxPoolSize to be exceeded.)

The number of Connections in a pool decreases whenever a pool tests a Connection and finds it to be broken (see Configuring Connection Testing below), or when a Connection is expired by the pool after sitting idle for a period of time, or for being too old (See Managing Pool Size and Connection Age.)



四.http://blog.sina.com.cn/s/blog_3e7ed9500100abzz.html

根据以上的一些解答,修改配置文件:
Xml代码 复制代码
  1. <property name="hibernate.connection.provider_class">  
  2.             org.hibernate.connection.C3P0ConnectionProvider   
  3.         </property>  
  4. <!-- 初始化默认为20 -->  
  5.                   <property name="initialPoolSize">1</property>  
  6.         <property name="c3p0.min_size">1</property>  
  7.         <property name="c3p0.max_size">500</property>  
  8.                      
  9.         <property name="c3p0.time_out">1800</property>  
  10. <!--每隔100秒检测连接是否可正常使用 -->      
  11.     <property name="c3p0.idle_test_period">1000</property>    
  12.         <property name="c3p0.validate">true</property>  
  13. <property name="hibernate.c3p0.acquire_increment">3</property>   


对于这种配置也不知道结果会怎么样?
不知道有没有什么测试方法能马上看到效果?

其实hibernate里支持的c3p0参数没有多少.更多的还得在c3p0.property文件中定义
http://www.mchange.com/projects/c3p0/index.html#c3p0_properties