Hibernate4 No Session found for current thread原因
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 配置文件中加入
程序代码并且在处理业务逻辑的类上采用注解
程序代码public class CustomerServiceImpl implements CustomerService {
@Transactional
public void saveCustomer(Customer customer) {
customerDaoImpl.saveCustomer(customer);
}
...
}
另外在 hibernate 的配置文件中,也可以增加这样的配置来避免这个错误:
程序代码Hibernate4 No Session found for current thread原因的更多相关文章
- SSH2 No Session found for current thread原因
Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for curre ...
- Hibernate4 No Session found for current thread
Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for curre ...
- Hibernate4集成spring4报错----No Session found for current thread
在编写一个Hibernate4集成spring4的小demo的时候出现了该错误: org.hibernate.HibernateException: No Session found for curr ...
- Spring+Hibernate4 Junit 报错No Session found for current thread
论坛上有另外一篇更全面的帖子,jinnianshilongnian写的:http://www.iteye.com/topic/1120924 本文的环境是: spring-framework-3.1 ...
- 关于spring3中No Session found for current thread!and Transaction的配置和管理(转)
今天我是特别的郁闷,本来项目做到一半,以前都好好的,结果下午就出现问题,苦逼的到现在才解决.它出现问题的时候都一声不坑, ,(天啦,现在才发现CSDN啥时候把QQ表情给整过来了)就在注册用户的时候,咦 ...
- org.hibernate.HibernateException: No Session found for current thread
spring.springmvc和hibernate整合 在sessionFactory.getCurrentSession()时,出现以下异常 No Session found for curren ...
- hibernate在使用getCurrentSession时提示no session found for current thread
大致错误片段 org.hibernate.HibernateException: No Session found for current thread at org.springframework. ...
- spring+hibernate整合:报错org.hibernate.HibernateException: No Session found for current thread
spring+hibernate整合:报错信息如下 org.hibernate.HibernateException: No Session found for current thread at o ...
- SSH dao层异常 org.hibernate.HibernateException: No Session found for current thread
解决方法: 在 接口方法中添加 事务注解 即可. public interface IBase<PK extends Serializable, T> { @Transactional v ...
随机推荐
- jquery实现全选、反选、不选
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8&quo ...
- [Hibernate] - EAGER and LAZY
Hibernate中的字段映射中的Fetch有两种方式:EAGER和LAZY Eager:全部抓取 Lazy:延迟抓取 如果在字段中声明为Eager,那么在取得当前Bean时,同时会抓取Bean中的关 ...
- Css3_必备10个东西
1.边框圆角(Border Radiuas) 这个是我们在平常很常用的吧,以前我在用div圆角的时候,特别特别的痛苦,不管是用CSS来画圆角,还是用图片来画圆角都不那么容易,但是现在好了,在CSS3中 ...
- java 生成随机数
本段代码是生成的六位随机数.也可修改生成任意位随机数. int[] array = {0,1,2,3,4,5,6,7,8,9}; Random rand = new Random(); for (in ...
- 界面显示这个时间格式的js代码: 2016年1月19日 星期二 乙未(羊)年 腊月初十
today=new Date();function initArray(){ this.length=initArray.arguments.length for(var i=0;i<this. ...
- vbox下android分辨率设置
VBoxManage setextradata "android" "CustomVideoMode1" "1280x800x16" 1. ...
- robotframework笔记21
创建输出 当执行测试,创建多个输出文件和所有的 都以某种方式相关测试结果. 本节讨论什么 输出创建.如何配置创建,以及如何 调整他们的内容. 不同的输出文件 本节解释了不同可以创建和输出文件 如何配置 ...
- 關於my97datepicker
原因的一篇是比較老的版本了 目前使用4.72 目前碰到一種情況就是使用了PopUpForm.js,也就是在頁面中彈出一個框,用來編輯,或者添加數據等功能. 使用知道時間會出現一種情況.時間顯示被ifr ...
- 小例子(二)、winform窗体间的关系
写一个关于winform窗体间的关系 1.登陆,思路:登陆后隐藏登陆窗体,关闭Form2时结束整个应用程序. //登陆窗体 private void button2_Click(object send ...
- 网络编程之socket(转)
“一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价 值,那网络中进程之间如何通信,如我们每天打开浏 ...