Hibernate4 与 spring3 集成Hibernate4 No Session found for current thread
2015-01-22 12:46
591 查看
Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for current thread”, 这个错误的原因,网上有很多解决办法, 但具体原因的分析,却没有多少, 这里转载一个原理分析:
SessionFactory的getCurrentSession并不能保证在没有当前Session的情况下会自动创建一个新的,这取决于CurrentSessionContext的实现,SessionFactory将调用CurrentSessionContext的currentSession()方法来获得Session。在Spring中,如果我们在没有配置TransactionManager并且没有事先调用SessionFactory.openSession()的情况直接调用getCurrentSession(),那么程序将抛出“No
Session found for current thread”异常。如果配置了TranactionManager并且通过@Transactional或者声明的方式配置的事务边界,那么Spring会在开始事务之前通过AOP的方式为当前线程创建Session,此时调用getCurrentSession()将得到正确结果。
然而,产生以上异常的原因在于Spring提供了自己的CurrentSessionContext实现,如果我们不打算使用Spring,而是自己直接从hibernate.cfg.xml创建SessionFactory,并且为在hibernate.cfg.xml
中设置current_session_context_class为thread,也即使用了ThreadLocalSessionContext,那么我们在调用getCurrentSession()时,如果当前线程没有Session存在,则会创建一个绑定到当前线程。
Hibernate在默认情况下会使用JTASessionContext,Spring提供了自己SpringSessionContext,因此我们不用配置current_session_context_class,当Hibernate与Spring集成时,将使用该SessionContext,故此时调用getCurrentSession()的效果完全依赖于SpringSessionContext的实现。
在没有Spring的情况下使用Hibernate,如果没有在hibernate.cfg.xml中配置current_session_context_class,有没有JTA的话,那么程序将抛出"No CurrentSessionContext configured!"异常。此时的解决办法是在hibernate.cfg.xml中将current_session_context_class配置成thread。
在Spring中使用Hibernate,如果我们配置了TransactionManager,那么我们就不应该调用SessionFactory的openSession()来获得Sessioin,因为这样获得的Session并没有被事务管理。
至于解决的办法,可以采用如下方式:
1. 在spring 配置文件中加入
程序代码
<tx:annotation-driven transaction-manager="transactionManager"/>
并且在处理业务逻辑的类上采用注解
程序代码
@Service
public class CustomerServiceImpl implements CustomerService {
@Transactional
public void saveCustomer(Customer customer) {
customerDaoImpl.saveCustomer(customer);
}
...
}
另外在 hibernate 的配置文件中,也可以增加这样的配置来避免这个错误:
程序代码
<property name="current_session_context_class">thread</property>
Hibernate Transaction 与 getCurrentSession() 关系
Spring 3.x 与Hibernate 4.x 整合遇到的问题,描述如下:
对数据库的 增加、删除、修改 操作需要Spring的事务支持,所以对service层的上述操作增加事务。applicationContext.xml配置如下:
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" read-only="false"/>
<tx:method name="save*" propagation="REQUIRED" read-only="false" />
<tx:method name="remove*" propagation="REQUIRED" read-only="false" />
<tx:method name="delete*" propagation="REQUIRED" read-only="false"/>
<tx:method name="modify*" propagation="REQUIRED" read-only="false"/>
<tx:method name="update*" propagation="REQUIRED" read-only="false" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
由于Spring 3.x 对 Hibernate 4.x 不提供 HibernateDaoSupport,所以在dao的实现层注入SessionFactory,从而通过
Session session = sessionFactory.getCurrentSession();
来获得当前的session。applicationContext.xm中 sessionFactory的HibernateProperties增加以下属性:
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
注意:和Spring2.x不同,不能为thread,否则报错:org.hibernate.HibernateException: save is not valid without active transaction
OK,以上配置在 增加、删除、修改 操作时,都能正确执行,事务也正常执行!
当执行 查询 操作时,不需要事务的支持,代码如下:
public List<Student> getStudentList(String str)
{
Session session = sessionFactory.getCurrentSession();
List<Student> list = null;
list = session.createQuery(str).list();
return list;
}
问题来了,报错:org.hibernate.HibernateException: No Session found for current thread
问题解决:几乎所有正常的操作都必须在transcation.isActive()条件下才能执行。get,load,save, saveOrUpdate,list都属于这类!
详情可以查看源码!
建议:当方法不需要事务支持的时候,使用 Session session = sessionFactory.openSession()来获得Session对象,问题解决!
SessionFactory的getCurrentSession并不能保证在没有当前Session的情况下会自动创建一个新的,这取决于CurrentSessionContext的实现,SessionFactory将调用CurrentSessionContext的currentSession()方法来获得Session。在Spring中,如果我们在没有配置TransactionManager并且没有事先调用SessionFactory.openSession()的情况直接调用getCurrentSession(),那么程序将抛出“No
Session found for current thread”异常。如果配置了TranactionManager并且通过@Transactional或者声明的方式配置的事务边界,那么Spring会在开始事务之前通过AOP的方式为当前线程创建Session,此时调用getCurrentSession()将得到正确结果。
然而,产生以上异常的原因在于Spring提供了自己的CurrentSessionContext实现,如果我们不打算使用Spring,而是自己直接从hibernate.cfg.xml创建SessionFactory,并且为在hibernate.cfg.xml
中设置current_session_context_class为thread,也即使用了ThreadLocalSessionContext,那么我们在调用getCurrentSession()时,如果当前线程没有Session存在,则会创建一个绑定到当前线程。
Hibernate在默认情况下会使用JTASessionContext,Spring提供了自己SpringSessionContext,因此我们不用配置current_session_context_class,当Hibernate与Spring集成时,将使用该SessionContext,故此时调用getCurrentSession()的效果完全依赖于SpringSessionContext的实现。
在没有Spring的情况下使用Hibernate,如果没有在hibernate.cfg.xml中配置current_session_context_class,有没有JTA的话,那么程序将抛出"No CurrentSessionContext configured!"异常。此时的解决办法是在hibernate.cfg.xml中将current_session_context_class配置成thread。
在Spring中使用Hibernate,如果我们配置了TransactionManager,那么我们就不应该调用SessionFactory的openSession()来获得Sessioin,因为这样获得的Session并没有被事务管理。
至于解决的办法,可以采用如下方式:
1. 在spring 配置文件中加入
程序代码
<tx:annotation-driven transaction-manager="transactionManager"/>
并且在处理业务逻辑的类上采用注解
程序代码
@Service
public class CustomerServiceImpl implements CustomerService {
@Transactional
public void saveCustomer(Customer customer) {
customerDaoImpl.saveCustomer(customer);
}
...
}
另外在 hibernate 的配置文件中,也可以增加这样的配置来避免这个错误:
程序代码
<property name="current_session_context_class">thread</property>
Hibernate Transaction 与 getCurrentSession() 关系
Spring 3.x 与Hibernate 4.x 整合遇到的问题,描述如下:
对数据库的 增加、删除、修改 操作需要Spring的事务支持,所以对service层的上述操作增加事务。applicationContext.xml配置如下:
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" read-only="false"/>
<tx:method name="save*" propagation="REQUIRED" read-only="false" />
<tx:method name="remove*" propagation="REQUIRED" read-only="false" />
<tx:method name="delete*" propagation="REQUIRED" read-only="false"/>
<tx:method name="modify*" propagation="REQUIRED" read-only="false"/>
<tx:method name="update*" propagation="REQUIRED" read-only="false" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
由于Spring 3.x 对 Hibernate 4.x 不提供 HibernateDaoSupport,所以在dao的实现层注入SessionFactory,从而通过
Session session = sessionFactory.getCurrentSession();
来获得当前的session。applicationContext.xm中 sessionFactory的HibernateProperties增加以下属性:
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
注意:和Spring2.x不同,不能为thread,否则报错:org.hibernate.HibernateException: save is not valid without active transaction
OK,以上配置在 增加、删除、修改 操作时,都能正确执行,事务也正常执行!
当执行 查询 操作时,不需要事务的支持,代码如下:
public List<Student> getStudentList(String str)
{
Session session = sessionFactory.getCurrentSession();
List<Student> list = null;
list = session.createQuery(str).list();
return list;
}
问题来了,报错:org.hibernate.HibernateException: No Session found for current thread
问题解决:几乎所有正常的操作都必须在transcation.isActive()条件下才能执行。get,load,save, saveOrUpdate,list都属于这类!
详情可以查看源码!
建议:当方法不需要事务支持的时候,使用 Session session = sessionFactory.openSession()来获得Session对象,问题解决!
相关文章推荐
- Hibernate4 与 spring3 集成之后 “No Session found for current thread”
- spring3 与 Hibernate4集成报错 No Session found for current thread
- Spring 3 和 Hibernate4 整合时遇到的 No Session found for current thread !
- 关于在Spring中Hibernate的No Session found for current thread问题
- Spring-Hibernate - No Session found for current thread
- Spring整合hibernate No Session found for current thread
- Hibernate: No Session found for current thread
- Hibernate4 No Session found for current thread原因
- Hibernate4 No Session found for current thread原因
- 关于异常: org.hibernate.HibernateException: No Session found for current thread
- Spring framework3.2整合hibernate4.1报错:No Session found for current thread
- org.hibernate.HibernateException: No Session found for current thread
- Spring framework3.2整合hibernate4.1报错:No Session found for current thread
- org.hibernate.HibernateException: No Session found for current thread
- SpringMVC 整合 hibernate 报错:No Session found for current thread
- Spring framework3.2整合hibernate4.1报错:No Session found for current thread
- Hibernate4 No Session found for current thread
- SpringMVC3+Hibernate4问题:org.hibernate.HibernateException: No Session found for current thread
- org.hibernate.HibernateException: No Session found for current thread
- org.hibernate.HibernateException: No Session found for current thread