日期:2014-05-20  浏览次数:20932 次

hibernate+spring中连接池c3p0不能释放连接
如题:
application.xml连接池配置:
XML code

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl">
            <value>jdbc:mysql://localhost/icrom?useUnicode=true&amp;characterEncoding=utf-8</value>        </property>
        <property name="user">
                      <value>root</value>
        </property>
        <property name="password">
            <value>12345678</value>
        </property>
        <property name="minPoolSize">
            <value>5</value>
        </property>
        <property name="maxPoolSize">
            <value>30</value>
        </property>
        <property name="initialPoolSize">
            <value>10</value>
        </property>
        <property name="maxIdleTime">
            <value>60</value>
        </property>
        <property name="acquireIncrement">
            <value>3</value>
        </property>
        <property name="maxStatements">
            <value>0</value>
        </property>
        <property name="maxStatementsPerConnection">
            <value>0</value>
        </property> 
        <property name="idleConnectionTestPeriod">
            <value>28680</value>
        </property>
        <property name="acquireRetryAttempts">
            <value>30</value>
        </property>
        <property name="breakAfterAcquireFailure">
            <value>false</value>
        </property>
        <property name="testConnectionOnCheckout">
            <value>false</value>
        </property>
    </bean>


程序中使用查询部分的代码:
Java code

public AbstractPersistentObject findById(Class<?> klass, Integer id) {
        log.debug("getting " + klass.getName() + " instance with id: " + id);
        try {
            AbstractPersistentObject instance = (AbstractPersistentObject) 
                    getHibernateTemplate()
                    .get(klass, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }


结果是每查询30次就一直阻塞在那里了,因为连接池的连接已经用完了!显示的日志是:
opening JDBC connection
acquire test -- pool size: 29; target_pool_size: 29; desired target? 30
incremented pending_acquires: 1
awaitAvailable(): com.mchange.v2.c3p0.impl.NewPooledConnection@280bca
trace com.mchange.v2.resourcepool.BasicResourcePool@6c01b9 [managed: 29, unused: 0, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@280bca)
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager@b52598.acquireResource() returning. 
trace com.mchange.v2.resourcepool.BasicResourcePool@6c01b9 [managed: 30, unused: 1, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@280bca)
decremented pending_acquires: 0
trace com.mchange.v2.resourcepool.BasicResourcePool@6c01b9 [managed: 30, unused: 0, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@280bca)
  .
  .
  .
  .
initializing non-lazy collections
after autocommit
transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!