在开发中,碰到到了Could not obtain transaction-synchronized Session for current thread异常,因此特意记录下。

一、问题的产生

        在使用Hibernate时碰到了如下异常:
        检查了代码发现是代码中用sessionFactory.getCurrentSession()这样获取session,导致抛了这异常。

二、问题的解决过程

(1)先看下Hibernate相关的配置
        Hibernate版本是4.3.7,这里先说明一下。Hibernate相关配置如下:
        
        通过这个配置,并没发现什么不妥的地方。
        注意:
            hibernate.current_session_context_class = thread  是在单独使用Hibernate时才需要的配置,这里不用配置。

(2)从网上找答案
        在网上找到一篇文章,很好的解释了为什么会报这个错。链接为:http://blog.csdn.net/honghailiang888/article/details/53423941
这文章里面有一段话提醒了我。文章关键知识截图如下
        

    重点知识:
    • (1)必须要将使用了sessionFactory.getCurrentSession()获取session的代码所在的方法加入到事务管理器中;否则获取不到session了。
    • (2)sessionFactory.getCurrentSession()是要基于事务的,才能实现session生命周期的管理。所以我们查询方法上用个只读事务就ok了。

     了解到这个知识点后,我去看了下公司的项目代码,发现没有配事务切面,也没有用注解事务。 所以我当时写的代码所在的方法是没有加入到事务管理器中的;因此也就报了这个异常。

(3)解决问题
        由于service方法没有加入到事务管理器中,因此我修改了些配置文件。使用的不是声明式事务,是用的注解事务,这样方便点。配置如下:
        
        在我刚刚报错的Service方法中,加上了注解@Transactional(readOnly=true)
        
       
    结果: 运行程序,一切正常

三、小结

        在之前的开发中,一直没碰到这个问题;突然在公司的一个小项目中碰到了这问题。回想之前的代码,service层的方法当时都是用事务切面切中的,所以不会报这个错。而现在这个项目,既没有配事务切面,也没用注解事务;所以才报这个错。
        在后续的开发中,记住:使用了sessionFactory.getCurrentSession()获取session时,需要事务的支持;也就是说这段代码所在的方法要么被事务切面切中,要么方法上面用了注解事务。
        
 
 
 

 

Could not obtain transaction-synchronized Session for current thread原因及解决方案的更多相关文章

  1. 记一次bug思考过程:HibernateException: Could not obtain transaction-synchronized Session for current thread

    场景:把从客户端提交的任务放到线程池执行 异常:HibernateException: Could not obtain transaction-synchronized Session for cu ...

  2. Hibernate4中使用getCurrentSession报Could not obtain transaction-synchronized Session for current thread

    架个spring4+hibernate4的demo,dao层直接注入的sessionFactory,然后用getCurrentSession方法获取session,然后问题来了,直接报错: Could ...

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

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

  4. 关于Could not obtain transaction-synchronized Session for current thread 这个异常。

    Could not obtain transaction-synchronized Session for current thread 这个异常之前非常让我头大.对于网上的各种说法都试了一下反正都不 ...

  5. org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread

    spring与hibernate整合报错 org.hibernate.HibernateException: Could not obtain transaction-synchronized Ses ...

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

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

  7. Hibernate4 No Session found for current thread原因

    Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for curre ...

  8. SSH2 No Session found for current thread原因

    Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for curre ...

  9. Could not obtain transaction-synchronized Session for current thread 错误的解决方法!

    BsTable bsTable = new BsTable(); // String time = request.getParameter("date"); String tim ...

随机推荐

  1. 二叉搜索树(Binary Search Tree)实现及测试

    转:http://blog.csdn.net/a19881029/article/details/24379339 实现代码:  Node.java  //节点类public class Node{ ...

  2. 带你从零学ReactNative开发跨平台App开发(一)

    ReactNative跨平台开发系列教程: 带你从零学ReactNative开发跨平台App开发(一) 带你从零学ReactNative开发跨平台App开发(二) 带你从零学ReactNative开发 ...

  3. Java IO流学习总结一:输入输出流

    Java IO流学习总结一:输入输出流 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/54292148 本文出自[赵彦军的博客] J ...

  4. Android应用耗电量统计,无需USB连接

    Android应用耗电量统计一直是一个很头疼的问题,手工统计耗时太长,自动化统计又不是非常精准(执行自动化代码需要通过USB连接,而USB又会充电,这就造成统计数据不准).后来从前辈那里得知可以通过a ...

  5. Oracle EBS OM 创建订单

    DECLARE l_header_rec OE_ORDER_PUB.Header_Rec_Type; l_line_tbl OE_ORDER_PUB.Line_Tbl_Type; l_action_r ...

  6. windows7环境下使用pip安装MySQLdb for python3.7

    1.首先,需要确定你已经安装了pip.在Python2.7的安装包中,easy_install.py和pip都是默认安装的.可以在Python的安装目录先确认,如果\Python37\Scripts里 ...

  7. MySQL的binlog2sql闪回

    从MySQL binlog解析出你要的SQL.根据不同选项,你可以得到原始SQL.回滚SQL.去除主键的INSERT SQL等. 用途=========== * 数据快速回滚(闪回)* 主从切换后新m ...

  8. Linux 下解压 rar 文件

    网上下载rar 压缩文件的使用,在linux在我们需要对其进行解压缩,这个时候,我们需要安装 rar相关的文件,来进行解压缩. 1. 下载 我们进入rarlab网站,进行下载 rar for linu ...

  9. python基础学习14----正则表达式

    正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑. 在python中正则表达式被封 ...

  10. [Spark SQL_3] Spark SQL 高级操作

    0. 说明 DataSet 介绍 && Spark SQL 访问 JSON 文件 && Spark SQL 访问 Parquet 文件 && Spark ...