博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hibernate在使用getCurrentSession时提示no session found for current thread
阅读量:4557 次
发布时间:2019-06-08

本文共 2255 字,大约阅读时间需要 7 分钟。

大致错误片段

org.hibernate.HibernateException: No Session found for current thread    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)

 

 

大致问题:hibernate在处理sessoin

  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.首先检查web.xml 的问题

是否配置了org.springframework.orm.hibernate4.support.OpenSessionInViewFilte

openSessionInVieFilter
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
openSessionInVieFilter
/web/*

 

OpenSessionInViewFilte的作用:Spring为我们解决Hibernate的Session的关闭与开启问题。 

 

2.检查spring事物配置路径是否正确

(以下列出一个案例)

 

 

3.如上面两个都正确的情况下考虑在sessionfactory中添加hibernate的属性,自动创建session

sessionFactory里面加上   

thread

 

转载于:https://www.cnblogs.com/hwaggLee/p/5604063.html

你可能感兴趣的文章
easy ui datagrid 让某行复选框不能选中
查看>>
第六周作业
查看>>
关于adb端口被占用的解决办法
查看>>
php 部分内置函数的使用
查看>>
字符串处理技巧
查看>>
归档及压缩命令
查看>>
Mybatis步骤
查看>>
WPF自定义控件之扩展原生控件
查看>>
《区块链100问》笔记整理——42~49问
查看>>
使用Jquery+EasyUI 进行框架项目开发案例讲解之二---用户管理源码分享
查看>>
深入理解计算机系统(1.4)---并发与并行、浅谈抽象
查看>>
函数依赖的公理化系统
查看>>
rabbitmq学习(四):利用rabbitmq实现远程rpc调用
查看>>
侯捷C++学习(二)
查看>>
EasyPlayer RTSP Android安卓播放器修复播放画面卡在第一帧bug
查看>>
web项目中全局常量的添加
查看>>
搬运工程 启动!
查看>>
局部加权回归(LWR) Matlab模板
查看>>
Connect to the DSP on C6A8168/DM8168/DM8148 using CCS
查看>>
hibernate在使用getCurrentSession时提示no session found for current thread
查看>>