---
title: "spring整合hibernate关于session的管理"
author: "duyunfengsolo"
date: "2009-08-21T15:01:10Z"
tags: ["DAO", "Hibernate", "Spring", "Tomcat", "配置管理"]
slug: "spring-hibernate-session"
draft: false
lastmod: "2009-09-27T14:30:40Z"
id: 454469
url: "https://solodu.iteye.com/blog/454469"
---

最近在测试oa系统的时候，发现若连接几次数据库后，tomcat就完全没反应，打开log文件，发现系统抛出java.lang.IllegalStateException: Pool not open的异常。在网上找到了问题及解决方法如下：

在使用Spring进行系统开发的时候，数据库连接一般都是配置在Spring的配置文件中，并且由Spring来管理的。在利用Spring + Hibernate进行开发时也是如此。下面是一个简单的Spring + Hibernate Dao的例子：

程序代码

Java代码 [![复制代码](http://sunxboy.iteye.com/images/icon_copy.gif)](http://sunxboy.iteye.com/blog/385534 "复制代码")

1. **public** **class** DaoReal **extends** HibernateDaoSupport **implements** Dao {
2. **public** List&lt;User&gt; getAll() {
3. **return** **super**.getHibernateTemplate().find("from User");
4. }
5. }

```
public class DaoReal extends HibernateDaoSupport implements Dao {
　　public List<User> getAll() {
　　　　return super.getHibernateTemplate().find("from User");
　　}
}
```

 

 

　　在上面的这个例子中，我们并没有关闭Session，但程序并没有任何问题，那是因为Spring已经帮我们关闭了。那么再看一个例子：

程序代码

Java代码 [![复制代码](http://sunxboy.iteye.com/images/icon_copy.gif)](http://sunxboy.iteye.com/blog/385534 "复制代码")

1. **public** **class** DaoReal **extends** HibernateDaoSupport **implements** Dao {
2. **public** List&lt;User&gt; getAll() {
3. **return** **super**.getSession().createQuery("from User").list();
4. }
5. }

```
public class DaoReal extends HibernateDaoSupport implements Dao {
　　public List<User> getAll() {
    return super.getSession().createQuery("from User").list();
　　}
}
```

 

 

　　这个例子会不会有问题呢？的确，上面的例子中隐藏了一个问题，数据库连接并没有被关闭，在我们的印象中这件事似乎应该是Spring的。程序执行后，好像也没有什么问题，但是连续执行该语句n次（n&lt;=最大连接数，如果没有指定最大连接数，那么默认为10次）后，系统处于等待状态，不会继续执行了，控制台上并没有输出任何信息。打开log文件，发现系统抛出java.lang.IllegalStateException: Pool not open的异常，无法打开连接。这说明系统连接池中所有的连接都在使用中。那么我们手动关闭Session后，应该就没有问题了吧！是这样吗？修改我们的例子，如下：

程序代码

Java代码 [![复制代码](http://sunxboy.iteye.com/images/icon_copy.gif)](http://sunxboy.iteye.com/blog/385534 "复制代码")

01. **public** **class** DaoReal **extends** HibernateDaoSupport **implements** Dao {
02. **public** List&lt;User&gt; getAll() {
03. Session s = **super**.getSession();
04. **try** {
05. **return** s.createQuery("from User").list();
06. } **finally** {
07. s.close();
08. }
09. ｝
10. }

```
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已经帮我们关闭了

Java代码 [![复制代码](http://sunxboy.iteye.com/images/icon_copy.gif)](http://sunxboy.iteye.com/blog/385534 "复制代码")

1. **return** **super**.getHibernateTemplate().find(hql);

```
return super.getHibernateTemplate().find(hql);
```

 

 

方案二：（我用这方法解决的）

设定HibernateTemplate的AllowCreate为True，允许创建一个新的session

 

Java代码 [![复制代码](http://sunxboy.iteye.com/images/icon_copy.gif)](http://sunxboy.iteye.com/blog/385534 "复制代码")

01. **public** **class** ItemDAOImpl **extends** HibernateDaoSupport **implements** ItemDAO {
02. **public** List queryAll() **throws** Exception {
03. Session session=**super**.getSession(**true**);
04. String hql="from Item as i";
05. **try**{
06. List l=session.createQuery(hql).list();
07. }**finally**{
08. session.close();
09. }
10. }
11. }

```
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。　  
修改后的代码如下：

Java代码 [![复制代码](http://sunxboy.iteye.com/images/icon_copy.gif)](http://sunxboy.iteye.com/blog/385534 "复制代码")

1. **public** **class** ItemDAOImpl **extends** HibernateDaoSupport **implements** ItemDAO {
2. **public** List queryAll() **throws** Exception {
3. Session session = **super**.getSession();
4. String hql = "from Item as i";
5. List l = session.createQuery(hql).list();
6. releaseSession(session);
7. }
8. }

```
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);

}
} 
```