出现No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here异常
问题描述:
public void save(BaseEntity baseEntity) {
Session session = null;
try {
session = currentSession();
session.save(baseEntity);
} catch (HibernateException ex) {
ex.printStackTrace();
}
}
当我执行该save方法时,抛出了该异常。
问题分析:
没有配置事务引起的问题。
解决办法:
我采用的是注解配置事务的方式,在XXX-servlet.xml中加上:
<tx:annotation-driven/>
<!-- <tx:annotation-driven transaction-manager="XXX"/> -->
这样就支持注解的方式配置事务了,这个标签会默认选择id为transactionManager的事务管理器(可以通过修改其transaction-Manager属性值来指定其他事务管理器)。
然后在application-config.xml(数据库的spring配置文件,即配置sessionFactory的地方)中加上:
<!-- 事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
就成功配置事务了。
然后在需要使用事务的方法上加上@Transactional就能解决异常抛出的问题了。
@Transactional
public void save(BaseEntity baseEntity) {
Session session = null;
try {
session = currentSession();
session.save(baseEntity);
} catch (HibernateException ex) {
ex.printStackTrace();
}
}
(在类级加上@Transactional好像会出其他的错误,有待进一步调试...)
出现No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here异常的更多相关文章
- 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 ...
- 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 creation of non-transactional one here
大概分析一般使用了注解才会报这方面的错 1.没有在spring的ApplicationContext.xml中开启注解事务 <!-- 开启注解事务 --> <tx:annotatio ...
- No Hibernate Session bound to thread, and configuration does not allow creat
No Hibernate Session bound to thread, and configuration does not allow creat 今天遇到这么一个错误,在网上差了很多都没有能解 ...
- 解决No Hibernate Session bound to thread, and configuration does not allow creat。。。
applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans ...
- org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not a
如果出现了这个错误,看看在使用Hibernate的时候有没有在事务的环境下执行操作!
- No Hibernate Session bound to thread, and configuration does not allow
今天晚上挺悲催的,遇到了这个问题花费我很长时间,现在总结如下: 到这这种情况的发生有两种情况: 1,没有配置事物只要在Spring配置文件中添加如下代码: <bean id="txMa ...
- org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not
遇到这个问题之前,我去百度和谷歌去搜索了一下.发现各种说法.可是针对我的项目而言,也就是公司的项目而言,这个问题的根源并不是是网上所说的那样. 最后是通过自己的想法做測试得到了解决. 1.首先说说我的 ...
随机推荐
- 给远程桌面发送“Ctrl+Alt+Delete”组合键
首先: 在运行里,输入osk, 打开软键盘 然后,这时先按下本地键盘的Ctrl和Alt键,再点远程"软键盘"的"Del"键,成功发送"Ctrl+Alt ...
- Hibernate 查询:HQL查询(Hibernate Query Languge)
HQL是一种面向对象的查询语言,其中没有表和字段的概念,只有类,对象和属性的概念. 使用HQL查询所有学生: public static void main(String[] args) { Sess ...
- Foundation--NSArray+NSMutableArray
C语言中数组只能存放一种类型 OC语言中数组可以存放多种类型,但是只能是对象类型,不可以是基本类型 我们实际中最好在一个数组中只存放一种类型的元素 nil表示数组的结尾,所以我们不能填写值为nil的对 ...
- [Linked List]Partition List
Total Accepted: 53879 Total Submissions: 190701 Difficulty: Medium Given a linked list and a value x ...
- c++ iostream 相关使用
索引: C++的iostream标准库介绍+使用详解(转) iostream格式化输出 c++ 字符串流 sstream(常用于格式转换) 关于#include <iomanip>中iom ...
- git 常用命令总结。
引用:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137396284551 ...
- web.xml中<load-on-start>n</load-on-satrt>作用
如下面一段配置,我们再熟悉不过了: 我们注意到它里面包含了这段配置:<load-on-startup>1</load-on-startup>,那么这个配置有什么作用呢? 作用如 ...
- Java路径问题最终解决方案—可定位所有资源的相对路径寻址
1.在Java项目中,应该通过绝对路径访问文件,以下为访问的常用方法: 第一种方法:类名.class.getResource("/").getPath()+文件名 第二种方法:Th ...
- JQuery>>>get/post赋值全局变量
JQuery' post&get: asynchronous. 2014-06-27 16:57:25 var client; function getClient(){ $.ajax ...
- python之函数的使用
备注:本篇文章主要讲一讲函数及集合的一些常用用法: 一.首先先看下,集合(set): 集合的特点:无序.不重复(这点跟字典有点像) <1>,在需要访问集合的时候,由于集合本身是无序的,所以 ...