java.lang.IllegalStateException:池未打开

时间:2013-10-31 10:41:48

标签: java spring hibernate jdbc jdbc-postgres

处理大型Db事务时 我遇到了以下问题:

java.lang.IllegalStateException: Pool not open
  at org.apache.commons.pool.BaseObjectPool.assertOpen(BaseObjectPool.java:78)
  at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:722)
  at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:95)
  at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
  at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
  at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:423)
  at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
  at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
  at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
  at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
at com.indictranstech.mbmc.common.utils.AuditTrailHibernateListener.onPostInsert(AuditTrailHibernateListener.java:133)
  at org.hibernate.action.EntityInsertAction.postInsert(EntityInsertAction.java:131)
  at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:110)
  at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
  at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
  at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
  at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
  at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
  at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
  at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
  at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
  at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
  at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
  at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
  at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:375)
  at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
  at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
  at $Proxy53.merge(Unknown Source)

数据库连接由HibernateTemplate管理。 而事务由Spring Transaction Management管理。 数据库是PostgreSQL。

当有5到10个交易时,相同的代码可以正常工作。

1 个答案:

答案 0 :(得分:-1)

您是否在项目中使用了这样的代码?

Session session = hibernateTemplate.getSessionFactory().openSession();

当你使用openSession()时,spring不会自动关闭它,你应该像这样使用它

HttpSession session = null;
session  = hibernateTemplate.getSessionFactory().getCurrentSession();
if(null == session) {
    session = hibernateTemplate.getSessionFactory().openSession();
}

你也可以使用hibernate.executeFind(新的HibernateCallback(){...})来避免这个问题