Hibernate中解决No Hibernate Session bound to thread问题
引用:忘了
首先是getCurrentSession()与openSession()的区别:
1、getCurrentSession()与openSession()的区别?
* 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()创建的session则不会
* 采用getCurrentSession()创建的session在commit或rollback时会自动关闭,而采用openSession()创建的session必须手动关闭
2、使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:
* 如果使用的是本地事务(jdbc事务)
<property name="hibernate.current_session_context_class">thread</property>
* 如果使用的是全局事务(jta事务)
<property name="hibernate.current_session_context_class">jta</property>
openSession() 与 getCurrentSession() 有何不同和关联呢?
在
SessionFactory 启动的时候, Hibernate 会根据配置创建相应的 CurrentSessionContext ,在
getCurrentSession() 被调用的时候,实际被执行的方法是CurrentSessionContext.currentSession() 。在 currentSession() 执行时,如果当前
Session 为空, currentSession
会调用 SessionFactory 的 openSession 。所以 getCurrentSession() 对于 Java EE 来说是更好的获取 Session 的方法。
那么当使用getCurrentSession()方法时报No Hibernate Session bound to thread问题该怎么办呢?(引用自http://www.cnblogs.com/ylhssn/p/5181149.html)
1、getCurrentSession()是必须提交事务的。所以在用到session.getCurrentSession()的这个方法一定是要配置声明式事务管理。
2、openSession()恰恰是与以上的方法想法,它不需要提交事务。但是他的资源必须手动关闭。
所以针对“No Hibernate Session bound to thread”异常的解决方案有两个:
方案一:将getCurrentSession()改为openSession()。
方案二:在sessionFactory bean中的hibernateProperties项中配置以下信息:
1 <prop key="hibernate.current_session_context_class">
2 thread
3 </prop>
4 <prop key="hibernate.transaction.factory_class">
5 org.hibernate.transaction.JDBCTransactionFactory
6 </prop>
在sessionFactory bean中添加以上两条配置信息后,再次运行程序,又报异常!
org.hibernate.HibernateException: createCriteria is not valid without active transaction
已知使用getCurrentSession()方法是必须提交事务的,而在SSH框架中是使用spring配置声明式事务管理的。
那接下来再次复习一下spring配置声明式事务管理知识,以便查找错误。
spring配置声明式事务管理
此处知识回顾引自“一个无聊的人”的“Spring声明式事务配置管理方法”,博主讲的很详细,推荐一看, 传送门
以下是博文内容截图:
除此之外还可以使用其他几种方式用spring管理事务,请自行查找资料学习。
仔细核对项目中的配置信息后,确定事务已配置无误,当前异常信息还是未解决,继续找资料,最终在“如果你报createSQLQuery is not valid without active transaction,请看这里”一文中找到解决方式:
将hibernate.current_session_context_class的值由Thread改为org.springframework.orm.hibernate2.SpringSessionContext
至此由使用getCurrentSession()方法引发的异常问题解决!
Hibernate中解决No Hibernate Session bound to thread问题的更多相关文章
- HibernateException: No Hibernate Session bound to thread
解决No Hibernate Session bound to thread 背景交代 在使用this.getHibernateTemplate().getSessionFactory().getCu ...
- spring事务管理出错。No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy ...
- No Hibernate Session bound to thread, and configuration does not allow creat
No Hibernate Session bound to thread, and configuration does not allow creat 今天遇到这么一个错误,在网上差了很多都没有能解 ...
- spring 管理事务配置时,结果 报错: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常
java.lang.IllegalStateException: No Hibernate Session bound to thread, and configuration does not al ...
- org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not a ...
- Hibernate Could not obtain transaction-synchronized Session for current thread问题处理
项目通过Hibernate查询时报出如下错误: Hibernate Could not obtain transaction-synchronized Session for current thre ...
- Hibernate中两种获取Session的方式
转自:https://www.jb51.net/article/130309.htm Session:是应用程序与数据库之间的一个会话,是hibernate运作的中心,持久层操作的基础.对象的生命周期 ...
- 关于Hibernate Could not obtain transaction-synchronized Session for current thread
转载自 http://blog.csdn.net/flyjiangs/article/details/51537381 最近几年一直再搞android,最近闲下来了,顺便玩一下web. 整了个最新版本 ...
- 解决No Hibernate Session bound to thread, and configuration does not allow creat。。。
applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans ...
随机推荐
- Apex语言(九)类的方法
1.方法 方法是对象的行为.如下表: 看书,编程,打球就是方法. 2.创建方法 [格式] 访问修饰符 返回值类型 方法名(形式参数列表){ 方法体; } 访问修饰符:可以为类方法指定访问级别. 例如, ...
- loadrunner安装方法
1.loadrunner安装网盘地址: http://pan.baidu.com/s/1hrP6mDQ 一般会提示:“vc2005_sp1_with_atl_fix_redist 2.确认时提示缺少 ...
- PHP 页面刷新与跳转的方法汇总
HTML meta标签 <meta http-equiv='content-type' content="text/html;charset=utf-8"/> 实现页面 ...
- vue 对图片进行拖拽到另一个位置
1.拖动元素代码: 使用html5原生拖拽属性,在需要拖拽的图片中添加draggable="true"属性,并使用v-on添加拖动事件 2.被放置的区域事件代码: 使用html5原 ...
- Orcale用户管理
类 ------表 对象----行 属性----列 软件开发流程: 需求调研 需求分析 概要分析 详细分析 编码 测试 上线 维护 论坛: 1.注册和登录 2.发帖,回帖(关注,浏览数) 用户:(昵称 ...
- 【ACM】hdu_zs1_1001_水仙花数_201307271504
水仙花数 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Total Submissio ...
- [bzoj1001][BeiJing2006]狼抓兔子_网络流_最小割转对偶图
狼抓兔子 bzoj-1001 BeiJing2006 Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还 ...
- Spring MVC-页面重定向示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_page_redirection.htm 说明:示例基于Spring MVC 4. ...
- linux 7.2 下安装maven
由于现有项目采用maven打包所以需要安装maven 1.创建目录 mkdir /maven cd /maven 2.下载解压maven,这里选择maven版本为3.5.3 wget http://m ...
- leetcode第一刷_Minimum Depth of Binary Tree
非常easy的题目.只是还是认为要说一下. 最小深度.非常快想到bfs,层序遍历嘛.本科的时候实在是没写过多少代码,一開始竟然想不到怎么保存一层的信息.后来想到能够压入一个特殊的对象,每次到达这个对象 ...