转载自 http://blog.csdn.net/flyjiangs/article/details/51537381

最近几年一直再搞android,最近闲下来了,顺便玩一下web。

整了个最新版本的SSH(hibernate5.1.0+spring4.2.6+struts-2.5)

在写dao实现类的时候碰到一个问题,

@Repository
public class UserDaoImpl implements IUserDao { @Autowired
private SessionFactory sessionFactory; private Session getSession(){
return sessionFactory.getCurrentSession(); }
...
}

用了sessionFactory.getCurrentSession()这个之后,会提示

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread 这个异常。(据说hibernate 4以上的版本才会有)

这里可以用Session session = sessionFactory.openSession(),然后代码中去关闭 session.close.当然为了偷懒的原则

必须不自己去管理session。让Spring容器自己去处理。

研究了一下。发现 只要在

applicationContext.xml 中追加

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <tx:annotation-driven transaction-manager="transactionManager"/>

然后再在实现类加上@Transactional

@Repository
@Transactional
public class UserDaoImpl implements IUserDao { @Autowired
private SessionFactory sessionFactory; private Session getSession(){
return sessionFactory.getCurrentSession(); }
...
}

这样问题就完美解决了。

关于Hibernate Could not obtain transaction-synchronized Session for current thread的更多相关文章

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

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

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

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

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

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

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

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

  5. Could not obtain transaction-synchronized Session for current thread原因及解决方案

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

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

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

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

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

  8. [原创]java WEB学习笔记77:Hibernate学习之路---Hibernate 版本 helloword 与 解析,.环境搭建,hibernate.cfg.xml文件及参数说明,持久化类,对象-关系映射文件.hbm.xml,Hibernate API (Configuration 类,SessionFactory 接口,Session 接口,Transaction(事务))

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  9. Hibernate 的Configuration、sessionFactory和session和transaction对象解释

    1.Configuration对象: Configuration conf=new Configuration(); conf.configure(); 1.1 到 src下面找到名称hibernat ...

随机推荐

  1. c#输出指定信息到文本文件中(追加方式)

    /// <summary> /// 输出指定信息到文本文件 /// </summary> /// <param name="msg">输出信息& ...

  2. [转载]oracle物化视图

    原文URL:http://lzfhope.blog.163.com/blog/static/636399220124942523943/?suggestedreading&wumii 环境or ...

  3. start with...connect by子句的浅用

    start with的用法,其基本语法如下: select … from tablename start with 条件1connect by 条件2where 条件3; 但是我在pl/sql中写入以 ...

  4. DevExpress v17.2新版亮点—Analytics Dashboard篇(一)

    用户界面套包DevExpress v17.2日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了Analytics Dashboard v17.2 的新功能,快来下载试用新版本! ...

  5. L220

    He must not allow this unusual barrier (obstacle) to stop him from fighting against the enemy.他绝不能让这 ...

  6. SaltStack自动化运维工具

    一.SaltStack的了解 SaltStack管理工具允许管理员对多个操作系统创建一个一致的管理系统,包括VMware vSphere环境. SaltStack作用于仆从和主拓扑.SaltStack ...

  7. Cookie注入实战(非SQL注入)

    cookie注入原理其实很简单,就是利用了session机制中的特性,只能说是特性,不能算是漏洞. 这里简单的说下原理,session的机制就相当于你有一张蛋糕店的会员卡,这张会员卡就是你浏览器中的c ...

  8. 查看Linux下系统资源占用常用命令

    一 top命令 1.作用top命令用来显示执行中的程序进程,使用权限是所有用户. 2.格式top [-] [d delay] [q] [c] [S] [s] [i] [n] 3.主要参数d:指定更新的 ...

  9. asp.net webform 当前上下文中不存在名称“__o”

    错误 CS0103 当前上下文中不存在名称“__o” 最近在搞一个webform项目, 再页面写了点<%%>代码, 结果编译下居然出现了这个错误 炸裂啊,  这是什么毛线, 看起来是 In ...

  10. tensorflow中的参数初始化方法

    1. 初始化为常量 tf中使用tf.constant_initializer(value)类生成一个初始值为常量value的tensor对象. constant_initializer类的构造函数定义 ...