java.lang.IllegalStateException: Pool not open解决方案
来源:互联网 发布:淘宝怎么开店步骤视频 编辑:程序博客网 时间:2024/06/06 04:03
在使用Spring进行系统开发的时候,数据库连接一般都是配置在Spring的配置文件中,并且由Spring来管理的。在利用Spring + Hibernate进行开发时也是如此。下面是一个简单的Spring + Hibernate Dao的例子:
public class DaoReal extends HibernateDaoSupport implements Dao { public List<User> getAll() { return super.getHibernateTemplate().find("from User"); }}在上面的这个例子中,我们并没有关闭Session,但程序并没有任何问题,那是因为Spring已经帮我们关闭了。那么再看一个例子:
public class DaoReal extends HibernateDaoSupport implements Dao { public List<User> getAll() {return super.getSession().createQuery("from User").list(); }}这个例子会不会有问题呢?的确,上面的例子中隐藏了一个问题,数据库连接并没有被关闭,在我们的印象中这件事似乎应该是Spring的。程序执行后,好像也没有什么问题,但是连续执行该语句n次(n<=最大连接数,如果没有指定最大连接数,那么默认为10次)后,系统处于等待状态,不会继续执行了,控制台上并没有输出任何信息。打开log文件,发现系统抛出java.lang.IllegalStateException: Pool not open的异常,无法打开连接。这说明系统连接池中所有的连接都在使用中。那么我们手动关闭Session后,应该就没有问题了吧!是这样吗?修改我们的例子,如下:
public class DaoReal extends HibernateDaoSupport implements Dao {public List<User> getAll() {Session s = super.getSession();try {return s.createQuery("from User").list();}finally {s.close();}}}执行n遍后依然停止响应。问题出在哪里呢?其实Spring的Session总是与某个线程绑定的,而这个线程往往就是承载Servlet或Jsp的那个线程,也就是说,它的生命周期scope是request的。在上面的例子中,我们利用getSession强制获得了Hibernate的 Session,这个Session可能是当前事务中之前使用过的,或者可能是一个新的,并不在当前事务中,Spring只对当前事务中的Session 进行关闭。
这里有3个解决方案:
方案一:
用此种方法,虽然没有手动关闭数据库连接,但spring已经帮我们关闭了
return super.getHibernateTemplate().find(hql);方案二:
设定HibernateTemplate的AllowCreate为True,允许创建一个新的session
public class ItemDAOImpl extends HibernateDaoSupport implements ItemDAO {public List queryAll() throws Exception {Session session=super.getSession(true);String hql="from Item as i";try{List l=session.createQuery(hql).list();}finally{session.close();}}}方案三:
Spring API:
geSession()是org.springframework.orm.hibernate3.support.HibernateDaoSupport 中的一个方法,
它可以从当前事务或者一个新的事务获得一个hibernate session.
通常使用releaseSession(org.hibernate.Session)方法与getSession()配合。
如果没有绑定线程,releaseSession关闭由这个DAO的SessionFactory创建的Hibernate Session。
修改后的代码如下:
public class ItemDAOImpl extends HibernateDaoSupport implements ItemDAO {public List queryAll() throws Exception {Session session = super.getSession(); String hql = "from Item as i"; List l = session.createQuery(hql).list(); releaseSession(session); } }
- java.lang.IllegalStateException: Pool not open解决方案
- android java.lang.IllegalStateException: database not open解决方案
- android java.lang.IllegalStateException: database not open解决方法
- java.lang.IllegalStateException: Already in the pool!
- 举例说明android中出现java.lang.IllegalStateException: database not open这种错误的原因及解决办法
- java.lang.IllegalStateException: Could not read resource entities-xhtml.properties. 解决方案
- java.lang.IllegalStateException: No output folder 解决方案
- java.lang.IllegalStateException: ApplicationEventMulticaster not initialized解决办法
- java.lang.IllegalStateException: ApplicationEventMulticaster not initialized
- java.lang.IllegalStateException: ApplicationEventMulticaster not initialized
- java.lang.IllegalStateException: JodaTime library not available - @DateTimeFormat not supported
- java.lang.IllegalStateException: java.io.FileNotFoundException错误解决方案
- java.lang.IllegalStateException异常:简单分析和简单解决方案
- 错误 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: closed 解决方案
- 安卓开发 java.lang.IllegalStateException isPlaying() 解决方案
- 异常:java.lang.IllegalStateException: Cannot deactivate transaction synchronization - not active
- java.lang.IllegalStateException: Can not perform this action after onSaveIns
- java.lang.IllegalStateException: Can not perform this action after onSaveInstance
- 窗口和视图
- perror() strerror() _strerror()
- JW FLV PLAYER 中文 去水印 无水印 反编译 破解版使用说明文档
- 对程序员最具影响的书籍
- PNG图片详解(转)
- java.lang.IllegalStateException: Pool not open解决方案
- JW FLV PLAYER 中文 去水印 无水印 反编译 破解版使用说明文档
- window 文件名去掉不合法字符
- 转Configure,Makefile.am, Makefile.in, Makefile文件之间的关系
- php5中this,self,parent的意义
- Cache的使用
- 关于Linux的快速启动(fastboot)和低功耗(low power)的学习记录
- 传值,引用,指针
- JW FLV PLAYER 破解去水印反编译无水印 参数使用中文版说明文档