引用:忘了

首先是getCurrentSession()与openSession()的区别:

1、getCurrentSession()与openSession()的区别?

  * 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()创建的session则不会
  * 采用getCurrentSession()创建的session在commit或rollback时会自动关闭,而采用openSession()创建的session必须手动关闭
2、使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:
  * 如果使用的是本地事务(jdbc事务)
    <property name="hibernate.current_session_context_class">thread</property>
  * 如果使用的是全局事务(jta事务)
    <property name="hibernate.current_session_context_class">jta</property>

openSession() 与 getCurrentSession() 有何不同和关联呢?

  在
SessionFactory 启动的时候, Hibernate 会根据配置创建相应的 CurrentSessionContext ,在
getCurrentSession() 被调用的时候,实际被执行的方法是CurrentSessionContext.currentSession() 。在 currentSession() 执行时,如果当前
Session 为空, currentSession
会调用 SessionFactory 的 openSession 。所以 getCurrentSession() 对于 Java EE 来说是更好的获取 Session 的方法。

那么当使用getCurrentSession()方法时报No Hibernate Session bound to thread问题该怎么办呢?(引用自http://www.cnblogs.com/ylhssn/p/5181149.html)

  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()方法引发的异常问题解决!

Hibernate中解决No Hibernate Session bound to thread问题的更多相关文章

  1. HibernateException: No Hibernate Session bound to thread

    解决No Hibernate Session bound to thread 背景交代 在使用this.getHibernateTemplate().getSessionFactory().getCu ...

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

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

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

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

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

  6. Hibernate Could not obtain transaction-synchronized Session for current thread问题处理

    项目通过Hibernate查询时报出如下错误: Hibernate Could not obtain transaction-synchronized Session for current thre ...

  7. Hibernate中两种获取Session的方式

    转自:https://www.jb51.net/article/130309.htm Session:是应用程序与数据库之间的一个会话,是hibernate运作的中心,持久层操作的基础.对象的生命周期 ...

  8. 关于Hibernate Could not obtain transaction-synchronized Session for current thread

    转载自 http://blog.csdn.net/flyjiangs/article/details/51537381 最近几年一直再搞android,最近闲下来了,顺便玩一下web. 整了个最新版本 ...

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

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

随机推荐

  1. OSI参考模型(转)

    一.OSI参考模型 自下而上:物理层(物理介质,比特流).数据链路层(网卡.交换机).网络层(IP协议).传输层(TCP/UDP协议).会话层(创建/建立/断开连接).表示层(翻译,编码,压缩,加密) ...

  2. centos6.5 安装Python3.6.0

      首先安装python3.6可能使用的依赖 # yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel ...

  3. 【编程工具】Vim编辑器的使用

    1.Vim简介   Vim最初起源于古老的贝尔实验室,由"Bram Moolenaar等人"开发,是一个功能强大的文本编辑器,被推崇为类Vi编辑器中最好的一个.   Vim是一个类 ...

  4. [转载]Linux内存高,触发oom-killer问题解决

    最近遇到两起Linux的内存问题,其一是触发了oom-killer导致系统挂 首先确认该系统的版本是32位 #uname -a Linux alarm 2.6.9-67.ELsmp #1 SMP We ...

  5. phpMyAdmin使用教程

    ---恢复内容开始--- wamp中自带了管理MySQL的phpMyAdmin,可用来本机测试,服务器维护,虚拟主机用户管理MySQL. 登录需记住servername,username,passwo ...

  6. phpqrcode生成二维码

    这篇文章讲解得非常详细: https://www.jb51.net/article/136418.htm 备注一下: 如果遇到生成的二维码是一串乱码.只需要在代码最后加上 exit();即可解决,原理 ...

  7. mysql 基础教程

    创建数据库: CREATE DATABASE --DATABASE 或者 SCHEMA数据库集合 IF NOT EXISTS db_name CHARACTER SET utf8 COLLATE ut ...

  8. spring boot架构浅谈

    首先来说一下什么是spring boot架构 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置 ...

  9. 在win7下,easyphp安装过程中MSVCR110.DLL没有被指定在WINDOWS上运行,或者它包含错误

    这是安装easyphp后启动时出现的错误界面.查网上一大堆说法,基本上p用没有.真正解决的办法去m$站点 http://www.microsoft.com/zh-CN/download/details ...

  10. 通用 mapper

    一.为什么需要通用 mapper 插件 通用 mapper 插件可以自动的生成 sql 语句. 虽然 mybatis 有逆向工程,可以直接生成 XxxMapper.xml 文件,但是这种生成的方式存在 ...