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. Linux大文件分割split和合并cat使用方法

    本文主要介绍linux下两个命令:split和cat.其中,相信大家都熟悉cat命令,一般用来查看一个文件的内容,但是它还其它的功能,比如这里要介绍的文件合并功能,它可把多个文件内容合并到一个文件中. ...

  2. linux特殊字符

    linux特殊字符: * 匹配文件名中的任何字符串,包括空字符串. ? 匹配文件名中的任何单个字符. [...] 匹配[ ]中所包含的任何字符. [!...] 匹配[ ]中非感叹号!之后的字符. 当s ...

  3. 转!!深入理解 Session 与 Cookie

    摘要 Session 与 Cookie 不管是对 Java Web 的初学者还是熟练使用者来说都是一个令人头疼的问题.在初入职场时恐怕很多程序员在面试的时候都被问到过这个问题.其实这个问题回答起来既简 ...

  4. static final的理解

    static: static静态,可以修饰类,成员变量,成员方法,代码块.static修饰的成员变量和方法独立于该类的任何对象,也就是被类的所有成员共享,这要这个类被加载,虚拟机就能根据类名在运行时数 ...

  5. 在腾讯云上创建您的SQL Cluster(2)

    版权声明:本文由李斯达原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/248 来源:腾云阁 https://www.qclo ...

  6. 【bzoj1053】反素数

    [bzoj1053]反素数 题意 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4.如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数.例 ...

  7. js(引用类型和setTimeout scope)

    题目是群中小伙伴出的. var a = [1,2,3]; c= a //todo 限制条件 c 不能出现在 = 左边 console.log(a) console.log(c) console.log ...

  8. (17)odoo方法和修饰器

    ---------------------更新时间:11:06 2016-09-27 星期二18:06 2016-09-18 星期日10:31 2016-03-01 星期二-------------- ...

  9. fork&exec

    进程是系统进行资源分配和调度的基本单位,包括代码.数据和PCB进程控制块等资源. fork函数通过系统调用创建一个与原进程相同的子进程. 在调用进程(父进程)中返回一次,返回子进程ID:在子进程返回0 ...

  10. 静态库 && 动态库

    http://weihe6666.iteye.com/blog/1100065 http://www.cnblogs.com/skynet/p/3372855.html 静态库: 在链接阶段,将汇编生 ...