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>

Hibernate4 No Session found for current thread的更多相关文章

  1. Hibernate4 No Session found for current thread原因

    Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for curre ...

  2. Spring+Hibernate4 Junit 报错No Session found for current thread

    论坛上有另外一篇更全面的帖子,jinnianshilongnian写的:http://www.iteye.com/topic/1120924 本文的环境是:  spring-framework-3.1 ...

  3. Hibernate4集成spring4报错----No Session found for current thread

    在编写一个Hibernate4集成spring4的小demo的时候出现了该错误: org.hibernate.HibernateException: No Session found for curr ...

  4. org.hibernate.HibernateException: No Session found for current thread

    spring.springmvc和hibernate整合 在sessionFactory.getCurrentSession()时,出现以下异常 No Session found for curren ...

  5. hibernate在使用getCurrentSession时提示no session found for current thread

    大致错误片段 org.hibernate.HibernateException: No Session found for current thread at org.springframework. ...

  6. 关于spring3中No Session found for current thread!and Transaction的配置和管理(转)

    今天我是特别的郁闷,本来项目做到一半,以前都好好的,结果下午就出现问题,苦逼的到现在才解决.它出现问题的时候都一声不坑, ,(天啦,现在才发现CSDN啥时候把QQ表情给整过来了)就在注册用户的时候,咦 ...

  7. spring+hibernate整合:报错org.hibernate.HibernateException: No Session found for current thread

    spring+hibernate整合:报错信息如下 org.hibernate.HibernateException: No Session found for current thread at o ...

  8. SSH2 No Session found for current thread原因

    Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for curre ...

  9. SSH dao层异常 org.hibernate.HibernateException: No Session found for current thread

    解决方法: 在 接口方法中添加 事务注解 即可. public interface IBase<PK extends Serializable, T> { @Transactional v ...

随机推荐

  1. [bzoj 1143]最长反链二分图最大匹配

    Dilworth定理:偏序集能划分成的最少的全序集的个数与最大反链的元素个数相等. 证明:http://www.cnblogs.com/itlqs/p/6636222.html 题目让求的是最大反链的 ...

  2. tomcat部署多个项目,通过域名解析访问不同的网站

    转摘自:http://qinyinbolan.blog.51cto.com/4359507/1211064 说明: 1.首先需要有多个域名,同时指向一个IP地址. 例如:域名:www.bbb.com, ...

  3. 【转载】How long is “too long” for MySQL Connections to sleep?

    From:http://dba.stackexchange.com/questions/1558/how-long-is-too-long-for-mysql-connections-to-sleep ...

  4. 计算1-1/x+1/x*x

    // algo1-1.cpp 计算1-1/x+1/x*x. #include<stdio.h> #include<sys/timeb.h> void main() { time ...

  5. WebView使用--文章集锦

    对于android WebView加载不出Html5网页的解决方法 在android4.4中webview的使用相对于之前版本的一些区别 理解WebKit和Chromium: Android 4.4 ...

  6. RPC-整体概念

    RPC概述 RPC(Remote Procedure Call),即远程过程调用,是一种通过网络从远程计算机程序上请求服务而不需要了解底层网络技术的协议,实现调用远程主机上的方法就像调用本地方法一样. ...

  7. bzoj4886 [Lydsy2017年5月月赛]叠塔游戏

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4886 [题解] 跟bzoj4883:http://www.cnblogs.com/galax ...

  8. JHDU 2601 An easy problem (数学 )

    title: An easy problem 数学 杭电2601 tags: [数学] 题目链接 Problem Description When Teddy was a child , he was ...

  9. 阻塞DOM

    DOM是解析后的HTML. 这些阻塞因素我们可以叫做 阻塞渲染的资源 ,例如 HTML.CSS(也包括web font)和 JavaScript. 请注意,图像是不会阻塞渲染的 ,所以如果有图像落在蓝 ...

  10. 全局axios默认值 和 自定义实例默认值

    首先说了一下情况, 登录后成功返回token 然后在带着token去继续下面的请求, 奇怪的是都是当前页面起作用,刷新和跳转之后就token 就消失了. 查了 axios文档发现 被自己坑了 我设置了 ...