如果你报createSQLQuery is not valid without active transaction,请看这里
原文:https://blog.csdn.net/yinjian520/article/details/8666695
很多时候我们使用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管理事务。
如果为thread 则会报异常,
原因还是spring中hibernate.current_session_context_class的问题
在spring的类LocalSessionFactoryBean源码,方法buildSessionFactory中将hibernate.current_session_context_class设为org.springframework.orm.hibernate3.SpringSessionContext
如果你报createSQLQuery is not valid without active transaction,请看这里的更多相关文章
- 编程异常——假设你报createSQLQuery is not valid without active transaction,...
非常多时候我们使用hibernate的session时,都是让session在某一执行环境中保持其唯一. 比如在同一线程内用同一个session.在同一方法内用同一session,这样我们就能够用se ...
- spring整合hibernate的时候报异常org.hibernate.HibernateException: createQuery is not valid without active transaction
在整合Spring4+hibernate4时候,当代码执行到dao中CRUD操作时,报了一个异常, org.hibernate.HibernateException: createQuery is n ...
- 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 ...
- org.hibernate.HibernateException: getFlushMode is not valid without active transaction
Spring & Hibernate 整合异常记录: org.hibernate.HibernateException: getFlushMode is not valid without a ...
- 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()来开启事务
随机推荐
- POCO库中文编程参考指南(5)Poco::Net::SocketAddress
1 枚举 最大地址长度,这个与Poco::Net::IPAddress中的定义可以类比,不过这里指的是`struct sockaddr_in6 enum { MAX_ADDRESS_LENGTH = ...
- 微信小程序 使用swiper制作一个滑动导航
最近在做一个导航的时候,发现使用overflow-x: auto来做多内容滑动导航效果很不好,思索是不是可以使用swiper来做一个,研究了下其实发现原理基本相同 这里说下,要用swiper做导航菜单 ...
- ASP.NET webFrom
web窗体的后缀名.aspx 1.<% %> 标签内的代码在服务器上执行 <body> <form id="form1" runat="se ...
- 2017 ACM-ICPC Asia Xi'an Problem A XOR(异或线性基 )
题目链接 2017西安赛区 Problem A 题意 给定一个数列,和$q$个询问,每个询问中我们可以在区间$[L, R]$中选出一些数. 假设我们选出来的这个数列为$A[i_{1}]$, $A[ ...
- spoj 913 Query on a tree II (倍增lca)
Query on a tree II You are given a tree (an undirected acyclic connected graph) with N nodes, and ed ...
- 小谈android/Linux rootkit(基于LKM)
最近又学习了一下,感觉还有好多东西不知道,以后积累多一点再从新写一个. 在android上捣鼓了一下linux的内核rootkit,虽然中途遇到了无数坑,至今也没有完全写完,打算先好好啃一段时间lin ...
- Ugly Number II -- LeetCode
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- 动态规划 001 - 编辑距离(Levenshtein Distance)问题
问题 字符串的编辑距离也被称为距Levenshtein距离(Levenshtein Distance),属于经典算法,常用方法使用递归,更好的方法是使用动态规划算法,以避免出现重叠子问题的反复计算,减 ...
- mysql truncate drop delete的区别
以下讨论,针对于mysql数据库. 为什么会想到这个问题呢? 因为项目中需要清除数据库的数据,而且需要实现自增的主键从0开始计数.所以想到总结一个几个常用的删除语法的差异. 可以做一个测试 建一个带有 ...
- NFS 服务配置篇
安装.配置NFS服务 1.NFS简介 NFS(network file system) NFS是一个主机A通过网络,允许其他主机B可以来共享主机A的一个目录文件的一个文件系统 2.需要安装两个包nfs ...