HibernateException: No Hibernate Session bound to thread

解决No Hibernate Session bound to thread

 背景交代

  在使用this.getHibernateTemplate().getSessionFactory().getCurrentSession()方法获取session时报以下异常信息:

org.hibernate.HibernateException: No Hibernate Session bound to thread, and conf  iguration does not allow creation of non-transactional one here 

在SSH架构中,spring如何对事务和hibernate session进行管理

  1、getCurrentSession()是必须提交事务的。所以在用到session.getCurrentSession()的这个方法一定是要配置声明式事务管理。

  2、openSession()恰恰是与以上的方法想法,它不需要提交事务。但是他的资源必须手动关闭。

  所以针对“No Hibernate Session bound to thread”异常的解决方案有两个:

    方案一:将getCurrentSession()改为openSession()。

    方案二:在sessionFactory bean中的hibernateProperties项中配置以下信息:

1 <prop key="hibernate.current_session_context_class">
2       thread
3 </prop>
4 <prop key="hibernate.transaction.factory_class">
5   org.hibernate.transaction.JDBCTransactionFactory
6 </prop>

   在sessionFactory bean中添加以上两条配置信息后,再次运行程序,又报异常!

org.hibernate.HibernateException: createCriteria is not valid without active transaction 

  已知使用getCurrentSession()方法是必须提交事务的,而在SSH框架中是使用spring配置声明式事务管理的。

  那接下来再次复习一下spring配置声明式事务管理知识,以便查找错误。

spring配置声明式事务管理

此处知识回顾引自“一个无聊的人”的“Spring声明式事务配置管理方法”,博主讲的很详细,推荐一看, 传送门

以下是博文内容截图:

  除此之外还可以使用其他几种方式用spring管理事务,请自行查找资料学习。

  仔细核对项目中的配置信息后,确定事务已配置无误,当前异常信息还是未解决,继续找资料,最终在“如果你报createSQLQuery is not valid without active transaction,请看这里”一文中找到解决方式:

  将hibernate.current_session_context_class的值由Thread改为org.springframework.orm.hibernate2.SpringSessionContext

  至此由使用getCurrentSession()方法引发的异常问题解决!

时间: 2024-08-28 04:09:25

HibernateException: No Hibernate Session bound to thread的相关文章

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

遇到这个问题之前,我去百度和谷歌去搜索了一下,发现各种说法,但是针对我的项目而言,也就是公司的项目而言,这个问题的根源并非是网上所说的那样. 最后是通过自己的想法做测试得到了解决. 1.首先说说我的配置吧!我的配置是通过spring自带的注解来实现 声明式事物管理的.如果我们没去了解spring的声明式事物管理的话,或许我们是得不出什么结论的. 如果你配置过声明式事物管理,你就知道spring是怎么帮你管理的. 2.spring声明式事物管理是在service层管理的,关于到sessionFac

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 allow creation of non-transactional one here这个异常 这个错误,网上说的原因一大推,反正我这个出的问题 是因为 虽然我在 spring 里把事务都配置好了,结果运行就出现这个错误,看看配置都没什么问题,都是网上 案例 照 写编码的,还是错的,结果发现是因为 我

springmvc No Hibernate Session bound to thread

最近整合springmvc 发现一个离奇的报错,No Hibernate Session bound to thread, and configuration does not allow creation,就是这丫,之前在单元测试的时候测试sessionFactory,测试transaction都通过,正当我happy 的跑去做action的时候,这丫来个晴天霹雳啊.......没有一点点防备,也没有一丝顾虑,他就这样出现,在我的代码里........因为在之前整合s2sh的时候从来没出现过,

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

applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://ww

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

今天遇到这么一个错误,在网上差了很多都没有能解决我的问题.transactional注解也添加了,各种配置也都没问题,结果快纠结死了. 最终在一个回复里面发现,原来是opensessionInView这个filter没有配置导致的. 具体原理不明,暂且记下来. <!-- Hibernate Open Session in View filter --> <filter> <filter-name>openSessionInViewFilter</filter-na

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方法时,抛出了该异常. 问题分析: 没有配置事务引起的问题. 解决办法: 我采用的是注解配置事务的

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

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

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

大概分析一般使用了注解才会报这方面的错 1.没有在spring的ApplicationContext.xml中开启注解事务 <!-- 开启注解事务 --> <tx:annotation-driven transaction-manager="txManager"/> 2.没有在方法上挂注解事务标签

[原理][来源解析]spring于@Transactional,Propagation.SUPPORTS,以及 Hibernate Session,以及jdbc Connection关联

Spring 捆绑Hibernate. 夹: 一.  1. Spring 怎样处理propagation=Propagation.SUPPORTS? 2. Spring 何时生成HibernateSession ? 3.  propagation=Propagation.SUPPORTS和propagation=Propagation.require对生成Session有何影响,共同点和差别 ? 3.1. 未配置@Transaction和 配置@Transaction(propagation=P