出现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.首先说说我的 ...
随机推荐
- Asp.Net使用Bulk批量插入数据
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Di ...
- android一些常用的代码1(收藏)
以下内容来自多个开源项目的整理和自己的项目积累. 1.拨打电话 public static void call(Context context, String phoneNumber) { conte ...
- Python 初学
一. 前言 不怕各位园友笑话,今年年初时,我才知道有一个叫python的编程语言,听说它很大强大,而我只会用c#,正想好好再学一门新语言,还有人分享自己的经验时说,使用python制作的脚本,再做持续 ...
- 关于Oracle中查询结果为未选定行
今天在做关于Oracle查询语句的练习时,碰到这么一个题目:找出EMP表中姓名(ENAME)第三个字母是A的员工姓名.我的Scott.emp表的现有数据如下: SQL> select * fro ...
- 整理js继承
实现继承的方法: 一,原型链:利用原型让一个引用类型继承另一个引用类型的属性和方法 function SuperType(){ this.property=true; } SuperType.prot ...
- java菜鸟篇<四> ZTree入门篇
今天准备入手ZTree,于是在百度上搜了搜,找到了开源网址和一些大神们的教程,于是乎下午开始了组织树(ZTree)的练习 初步完整的作品是这个样子的: 1.咱们要去这个工具的开源网里找下载的东西: ( ...
- Lua编译
编译lua包含3部分内容:lua库文件(lua*.lib),lua解释器(lua.exe),lua编译器(luac.exe) 首先: 下载源代码,编译批处理(以5.2.3为例): cd srccl / ...
- Android Fragment(碎片)的使用
简介 在Android中Fragment为一种可以嵌入活动中的UI片段.能让程序更加合理地利用大屏幕的空间. 使用方法 1.我们首先新建的一个onefragment.xml文件. <?xml v ...
- Android 贝塞尔曲线
博客图片备份位置:
- MySQL命令记录1
mysql命令行 开启:net start mysql56关闭:net start mysql56(这两种情况必须有管理员权限) 登陆:mysql -h localhost -u root -p(lo ...