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. SSH2 No Session found for current thread原因

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

  2. Hibernate4 No Session found for current thread

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 转:c的回归-云风

    C 的回归 周末出差,去另一个城市给公司的一个项目解决点问题.回程去机场的路上,我用手机上 google reader 打发时间.第一眼就看到孟岩大大新的一篇:Linux之父话糙理不糙 .主题是 C ...

  2. Vbs脚本经典教材(转)

    Vbs脚本经典教材(最全的资料还是MSDN) —为什么要使用Vbs? 在Windows中,学习计算机操作也许很简单,但是很多计算机工作是重复性劳动,例如你每周也许需要对一些计算机文件进行复制.粘贴.改 ...

  3. entity refenrece 在views中的运用

    在一个content type中有一个field是entity reference, 那么这个字段的设置过程中会指定一个entity type和content type和一个具体内容的选择器, 然后到 ...

  4. phalcon: (非官方)简单的多模块

    phalcon: [非官方]多模块 配合router使用 例如:我的模块叫做: home 入口文件增加引入: use Phalcon\Mvc\Router; 在自动引入前面增加,自动引入命名空间: / ...

  5. java 多线程4(死锁)

    死锁现象: 死锁原因: 1.存在两个或两个以上的线程. 2.存在两个或两个或两个以上的共享资源. 死锁现象解决的方案: 没有方案只能尽量避免.

  6. table合并单元格colspan和rowspan .

    colspan和rowspan这两个属性用于创建特殊的表格. colspan是“column span(跨列)”的缩写.colspan属性用在td标签中,用来指定单元格横向跨越的列数: 在浏览器中将显 ...

  7. 调用webservice 417

    在调用webservice时有些服务器一直返回“远程服务器返回错误: (417) Expectation failed”  这个提示,解决方案: 在调用代码的最开始加入如下一句:System.Net. ...

  8. 实现 像网易云音乐 播放列表那样的弹出型Dialog

    如图 所示是点击Test之后的 弹出的Dialog (请无视我工程的命名) 20161017/**加入点击回调,假设dialog里放了一个TextView*/ 得先写一个点击回调 public int ...

  9. 三张图彻底了解Java中字符串的不变性

    转载: 三张图彻底了解Java中字符串的不变性 定义一个字符串 String s = "abcd"; s中保存了string对象的引用.下面的箭头可以理解为"存储他的引用 ...

  10. 《Java程序设计》实验三 实验报告

    实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程 2 ...