首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

hibernate+spring中连接池c3p0不能释放连接,该如何解决

2012-02-06 
hibernate+spring中连接池c3p0不能释放连接如题:application.xml连接池配置:XML codebean iddataSource

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!


after transaction completion
Eagerly flushing Hibernate session
flushing session
processing flush-time cascades
dirty checking collections
Flushing entities and processing referenced collections
Processing unreferenced collections
Scheduling collection removes/(re)creates/updates
Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
listing entities:
cn.icrom.biz.datapackage.news.object.NEWS_DATA_EXISTS{id=component[ID,DATA_FID,KODE,PUBLISH_DATE]{DATA_FID=9340, KODE=33cca49be6f395c739fa793b6bba0f5d, PUBLISH_DATE=2010-03-25 00:00:00, ID=9334}}
executing flush
registering flush begin
registering flush end
post flush
Registering Hibernate Session for deferred close
setting flush mode to: NEVER
Read class cn.icrom.biz.datapackage.news.object.NEWS_DATA detail data.
Returning cached instance of singleton bean 'BaseHibernateDAO'
getting cn.icrom.biz.datapackage.news.object.NEWS_DATA instance with id: 9340
Opening Hibernate Session
opened session at timestamp: 12754676400
loading entity: [cn.icrom.biz.datapackage.news.object.NEWS_DATA#9340]
attempting to resolve: [cn.icrom.biz.datapackage.news.object.NEWS_DATA#9340]
object not resolved in any cache: [cn.icrom.biz.datapackage.news.object.NEWS_DATA#9340]
Fetching entity: [cn.icrom.biz.datapackage.news.object.NEWS_DATA#9340]
loading entity: [cn.icrom.biz.datapackage.news.object.NEWS_DATA#9340]
about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
opening JDBC connection
acquire test -- pool is already maxed out. [managed: 30; max: 30]


求解呀!
请教对这连接池比较了解的朋友,怎么来解决这问题?是哪里有问题的呢?

[解决办法]
我前段时间也遇到过这个问题。
我后来检查发现,原来是程序中获取session进行操作后没有关闭,导致占用数据库连接没有释放。后来用完session把它给close了,就不再出现这个问题了。
说明一下:如果有用spring的声明式事务的话,那么通过this.getSesson()获取到的session是不需要关闭的,spring的事务会去管理。

楼主你的问题好像也应该差不多是这样
英文我看不太懂,不过我估计你的错误信息中下面这一句是关键,你从这入手去查查。
transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
[解决办法]
1。你的配置信息是配置在spring主配置文件里的,所以spring会帮你关闭数据库连接
2。spring会在c3p0被使用后的基础上进行关闭。

热点排行