C3p0使用时出现的异常及解决方案

当并发量大的时候,C3P0连接池会出现如下异常信息:

Caused by: java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.  
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)  
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:65)  
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:527)  
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)  
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:113)  
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79)  
... 5 more  
Caused by: com.mchange.v2.resourcepool.TimeoutException: A client timed out while waiting to acquire a resource from -- timeout at awaitAvailable()  
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1317)  
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)  
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)  
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)  
... 8 more 

对于这部分的异常分析如下:
网上很多说是C3P0的bug问题。c3p0在同时关闭statement和connection的时候,或者关闭他们之间的时间很短的时候,有时候connection并没有被关闭,因为有些preparedstatement还在被cached住。这样就会有很多connection并没有真正的被关闭,连接池的连接都给耗尽了,就会产生上面的异常。解决的方案就是把缓存关闭也就是把c3p0.max_statements 设置成0,这样就不会有缓存的preparedstatement,而设置的c3p0.idle_test_period又小于c3p0.timeout,这样的设置应该没有什么问题了。
回头有时间的话,再拜读下源码研究下,目前先这么解决即可.

你可能感兴趣的:(连接池)