出现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.首先说说我的 ...
随机推荐
- PHP学习笔记八【数组】
<?php //定义数组 $hens[0]=3; $hens[1]=5; $hens[2]=1; $hens[3]=3.4; $hens[4]=2; $hens[5]=50; //遍历整个数组 ...
- java替换字符串和用indexof查找字符
java自带替换 String s="hlz_and_hourui哈哈"; String new_S=s.replaceAll("哈", "笑毛&qu ...
- C# 中的常用正则表达式大全
这是从网上找来的,收藏一下备用,用到之处可以节省不少时间哦! 只能输入数字: "^[0-9]*$" . 只能输入n位的数字:"^\d{n}$". 只能输入 ...
- Java之利用Socket获取网站内容
public void main(String[] args){ String host = "123.126.113.42"; int port = 80; Socket s = ...
- javascript. String方法扩张.
有时候觉得js内置的方法不够用.可自己扩充. 下面是我扩充的几个方法. 为了避免变量冲出.覆盖.利用自执行函数实现. +function (a) { a.fn = a.prototype; var f ...
- [转]printf 字符串格式化
在将各种类型的数据构造成字符串时,sprintf 的强大功能很少会让你失望.由于sprintf 跟printf 在用法上几乎一样,只是打印的目的地不同而已,前者打印到字符串中,后者则直接在命令行上输出 ...
- 痛苦的版本对齐(3) cygwin下的路径引用
[续<痛苦的版本对齐(2) 和时间的相关性>]http://www.cnblogs.com/yvivid/p/3541142.html 初步定位,如下告警为.depend文件路径问题导致. ...
- 四巧工作简化法(ECRS)
在构思项目的工作方法时,可以运用ECRS(四巧工作简化法)技术,即E取消.C合并.R重排.S简化等四种技巧. 1.取消(Eliminate) 对所做的项目,首先应当考虑取消的可能性.如果所做的项目.工 ...
- 用 for/in 在 Java 5.0 中增强循环
这个方便的构造提供了什么?什么时候适用于代码? Brett McLaughlin (brett@newInstance.com), 作者/编辑, O'Reilly Media, Inc. 简介: fo ...
- 处理编译错误"0" is an invalid value for the "DebugInformation" parameter of the "DCC"
在编译“MustangpeakCommonLib-master.zip”时,遇到了这个问题.网上搜了一下: 处理编译错误"0" is an invalid value for the "DebugIn ...