问题描述:

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异常的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 报错解决:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

    大概分析一般使用了注解才会报这方面的错 1.没有在spring的ApplicationContext.xml中开启注解事务 <!-- 开启注解事务 --> <tx:annotatio ...

  5. No Hibernate Session bound to thread, and configuration does not allow creat

    No Hibernate Session bound to thread, and configuration does not allow creat 今天遇到这么一个错误,在网上差了很多都没有能解 ...

  6. 解决No Hibernate Session bound to thread, and configuration does not allow creat。。。

    applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans ...

  7. org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not a

    如果出现了这个错误,看看在使用Hibernate的时候有没有在事务的环境下执行操作!

  8. No Hibernate Session bound to thread, and configuration does not allow

    今天晚上挺悲催的,遇到了这个问题花费我很长时间,现在总结如下: 到这这种情况的发生有两种情况: 1,没有配置事物只要在Spring配置文件中添加如下代码: <bean id="txMa ...

  9. org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not

    遇到这个问题之前,我去百度和谷歌去搜索了一下.发现各种说法.可是针对我的项目而言,也就是公司的项目而言,这个问题的根源并不是是网上所说的那样. 最后是通过自己的想法做測试得到了解决. 1.首先说说我的 ...

随机推荐

  1. 测试 windows live writer

    This is the first article written by the writer!   wenzhaoshanda

  2. java菜鸟篇<二> eclipse启动tomcat报错的问题:Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "main"

    9.1今天不知道自己瞎搞eclipse的时候按到了什么键,然后再启动程序的时候就会报错: 如下: Exception: java.lang.OutOfMemoryError thrown from t ...

  3. Android 轮询之 Service + AlarmManager+Thread (转)

    android中涉及到将服务器中数据变化信息通知用户一般有两种办法,推送和轮询. 消息推送是服务端主动发消息给客户端,因为第一时间知道数据发生变化的是服务器自己,所以推送的优势是实时性高.但服务器主动 ...

  4. view import symbols

    nm -D [file]|grep -w U or objdump -T [file]|grep "*UND*"

  5. AngularJs中关于ng-class的三种使用方式说明

    在开发中我们通常会遇到一种需求:一个元素在不同的状态需要展现不同的样子. 而在这所谓的样子当然就是改变其css的属性,而实现能动态的改变其属性值,必然只能是更换其class属性 这里有三种方法: 第一 ...

  6. JSP作为img的src时chrome的bug

    废话不说,直接上代码. 新建一个jsp文件,名为img.jsp,代码如下: <%@page import="javax.imageio.ImageIO"%> <% ...

  7. gnome-ssh-askpass:No such file or directory && unable to read askpass response

    1.使用git的时候,gnome-ssh-askpass出现“error: cannot run /usr/libexec/openssh/gnome-ssh-askpass: No such fil ...

  8. bzoj 1188 : [HNOI2007]分裂游戏 sg函数

    题目链接 给n个位置, 每个位置有一个小球. 现在两个人进行操作, 每次操作可以选择一个位置i, 拿走一个小球.然后在位置j, k(i<j<=k)处放置一个小球. 问你先进行什么操作会先手 ...

  9. 工作无聊,闲来无事,自己学习 android入门

    工作无聊,闲来无事,自己学习. 最近几天看了看有关android的UI设计,布局以及android有关控件的知识,算是进一步了解了 android的相关内容. 明天就是周末了,明天及后天,我打算开始学 ...

  10. 在 Inno Setup 中实现倒数N秒后激活按钮

    原文 http://restools.hanzify.org/article.asp?id=67 timectrl.dll 为一个 6.5 KB 的按钮激活时间控制插件.  引用来自 Example1 ...