编程异常——假设你报createSQLQuery is not valid without active transaction,...
非常多时候我们使用hibernate的session时,都是让session在某一执行环境中保持其唯一。
比如在同一线程内用同一个session。在同一方法内用同一session,这样我们就能够用session里面缓存好的数据。但。我想说的不是缓存,且听我一一道来。
近期试用spring3.0.2+struts2.18+hibernate3.3.2学习搭建一个web项目,出现了一个相当郁闷的问题。
就是我明明配置好了spring管理hibernate事务了,当我在dao中运行hibernate的方法时。如save。delete,update,createQuery,总是说不能在没有活动的事务中运行(org.hibernate.HibernateException:
createSQLQuery is not valid without active transaction)。立刻上google查。一无所获。曾几度怀疑是否配置写出了。dao或service写错了,改来改去的依然存在问题。当时相当郁闷啊,想啊,你spring不是帮我管理事务么?你不自己主动开启事务啊,还要我手动开启啊。立刻查spring文档。从中文到英文。没发现什么有參考价值的线索,真是相当的打击。代码乱改一通。发现用spring的hibernatetemplate来进行数据操作又正常无比。不死心的去查了hibernate的doc,一个不留神给哥发现了一个冗长的配置属性:hibernate.current_session_context_class。心里巨爽无比,就是你丫啦。小样的。哥把你灭了。
hibernate.current_session_context_class是做什么用的呢?通俗点来讲。就是配置session绑定到某一执行环境,比如从同一个线程中用getCurrentSession()取得的session都是同一个。当前没有session就自己主动创建一个返回给你丫用。问题就出在这里了。官方文档例如以下说:
使用 Hibernate 的大多数应用程序须要某种形式的“上下文相关的”会话。特定的会话在整个特
定的上下文范围内始终有效。然而,对不同类型的应用程序而言,要为什么是组成这种“上下文”下一个定义一般是困难的。不同的上下文对“当前”这个概念定义了不同的范围。在 3.0 版本号之前,使用 Hibernate 的程序要么採用自行编写的基于 ThreadLocal 的上下文会话。要么採用HibernateUtil 这种辅助类,要么採用第三方框架(比方
Spring 或 Pico),它们提供了基于代理(proxy)或者基于拦截器(interception)的上下文相关的会话。
从 3.0.1 版本号開始,Hibernate 添加了SessionFactory.getCurrentSession() 方法。
一開始,它假定了採用 JTA 事务。JTA 事务定义了当前 session 的范围和上下文(scope 和 context)。由于有好几个独立的 JTA TransactionManager 实现稳定可用,不论是否被部署到一个 J2EE 容器中,大多数(假若不是全部的)应用程序都应该採用
JTA 事务管理。基于这一点。採用 JTA 的上下文相关的会话能够满足你一切须要。
再来看我的配置,讲hibernate.current_session_context_class的值设成thread。
按我简单的理解就是将getCurrentSession()返回的session绑定到当前执行线程中。比較专业的说法是此session的上下文是thread,但不是spring已经托管的那个Session对象。再用哥那大腿想了几下,瞬间了解了一些。所以获取的session是在spring代理的上下文之外的的当前线程之中。所以此session并不是事务管理器代理的那个session,不会自己主动开启事务。依据官方提示:第三方框架提供了基于代理(proxy)或者基于拦截器(interception)的上下文相关的会话的管理,所以把hibernate.current_session_context_class设置删除了。一切又回到当初风平浪静的日子了。
參考http://justsee.iteye.com/blog/1061576,最终了解这个问题的前因后果。摘录例如以下:
在ssh2中的sessionFactory配置文件里应将hibernate.current_session_context_class设为org.springframework.orm.hibernate3.SpringSessionContext(默觉得此值)。并应用spring管理事务。
假设为<prop key="hibernate.current_session_context_class">thread</prop> 则会报异常,
原因还是spring中hibernate.current_session_context_class的问题
在spring的类LocalSessionFactoryBean源代码。方法buildSessionFactory中将hibernate.current_session_context_class设为org.springframework.orm.hibernate3.SpringSessionContext
传智播客成都java培训中心:http://cd.itcast.cn?140831ls
编程异常——假设你报createSQLQuery is not valid without active transaction,...的更多相关文章
- 如果你报createSQLQuery is not valid without active transaction,请看这里
原文:https://blog.csdn.net/yinjian520/article/details/8666695 很多时候我们使用hibernate的session时,都是让session在某一 ...
- spring整合hibernate的时候报异常org.hibernate.HibernateException: createQuery is not valid without active transaction
在整合Spring4+hibernate4时候,当代码执行到dao中CRUD操作时,报了一个异常, org.hibernate.HibernateException: createQuery is n ...
- org.hibernate.HibernateException: getFlushMode is not valid without active transaction
Spring & Hibernate 整合异常记录: org.hibernate.HibernateException: getFlushMode is not valid without a ...
- Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction
在spring4+hibernate4整合过程中,使用@Transactional注解事务会报"Exception in thread "main" org.hibern ...
- spring中使用Hibernate中的getCurrentSession报出:createQuery is not valid without active transaction
1.错误信息 HTTP Status 500 - createQuery is not valid without active transaction type Exception report m ...
- save is not valid without active transaction
org.hibernate.HibernateException: save is not valid without active transaction at org.hibernate.cont ...
- 异常:getHibernateFlushMode is not valid without active transaction; nested exception is org.hibernate.HibernateException: getHibernateFlushMode is not valid without active transaction getHibernateFlu
场景: 在使用spring整合hibernate调用的HibernateTemplate时报错解决: 在spring配置文件中添加事务的配置 <bean id="hibernateTr ...
- Hibernate 与Spring整合出现 hibernate.HibernateException: createCriteria is not valid without active transaction
当 Hibernate 和 Spring 整合时,在 Spring 中指定的 Hibernate.cfg.xml 文件内容中要注释掉以下内容: <!-- Enable Hibernate's a ...
- Exceprtion:e createQuery is not valid without active transaction; nested exception is org.hibernate.HibernateException: createQuery is not valid without active transaction
如果增加配置了current_session_context_class属性,查询的时候需要session.beginTrasaction()来开启事务
随机推荐
- cookie跨域,跨目录访问及单点登录。
首先普及下域名的知识: 域名: baidu.com // 一级域名 A play.baidu.com // 二级域名 B abc.play.baidu.com // 三级域名 C 数有几 ...
- OpenCV安装要点
OpenCV安装要点1.设置系统和用户环境变量PATH指向opencv\build\x86\vc10\bin或者opencv\build\x64\vc10\bin2.新建用户环境变量OpenCV指向o ...
- HTTP Post请求过程详解
摘要: HTTP(HyperText Transfer Protocol),超文本传输协议,是一个客户端和服务器端请求和应答的标准(TCP),客户端是终端用户,服务器端是网站. HTTP是基于Sock ...
- DouNet学习_Excel导入导出
Excel --->列是有限的 -->数据靠在单元格右边是数字类型,左边是字符串类型 把一个数字当初字符串来显示 在前面加个 ' -->用程序操作Excel 可以使用Excel的所有 ...
- kali2 ssh
vi /etc/ssh/sshd_config 1.将#PasswordAuthentication no的注释去掉,并且将NO修改为YES 2.将#PermitRootLogin without-p ...
- Linux配置静态IP
在一块SSD的CentOS配置静态IP 1. 配置静态IP #vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE="eth0" ...
- 《学习OpenCV》练习题第五章第一题ab
这道题是载入一幅带有有趣纹理的图像并用不同的模板(窗口,核)大小做高斯模糊(高斯平滑),然后比较用5*5大小的窗口平滑图像两次和用11*11大小的窗口平滑图像一次是否接近相同. 先说下我的做法,a部分 ...
- WebRtc VoiceEngine代码解析
WebRtc中VoiceEngine可以完成大部分的VOIP相关人物,包括采集.自动增益.噪声消除.回声抑制.编解码.RTP传输.下边我们通过代码来解析Voe中处理流程: 创建VoiceEngine和 ...
- Chocolatey的安装与使用
@(编程) 前言 在 Linux 下,大家喜欢用 apt-get 来安装应用程序,如今在 windows 下,大家可以使用 Chocolatey 来快速下载搭建一个开发环境. Chocolatey 的 ...
- Java断言assert
public class Welcome{ public static void main(String[] args){ assert false; System.out.println(" ...