关于Hibernate Could not obtain transaction-synchronized Session for current thread
转载自 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的更多相关文章
- org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
spring与hibernate整合报错 org.hibernate.HibernateException: Could not obtain transaction-synchronized Ses ...
- Hibernate Could not obtain transaction-synchronized Session for current thread问题处理
项目通过Hibernate查询时报出如下错误: Hibernate Could not obtain transaction-synchronized Session for current thre ...
- 记一次bug思考过程:HibernateException: Could not obtain transaction-synchronized Session for current thread
场景:把从客户端提交的任务放到线程池执行 异常:HibernateException: Could not obtain transaction-synchronized Session for cu ...
- Hibernate4中使用getCurrentSession报Could not obtain transaction-synchronized Session for current thread
架个spring4+hibernate4的demo,dao层直接注入的sessionFactory,然后用getCurrentSession方法获取session,然后问题来了,直接报错: Could ...
- Could not obtain transaction-synchronized Session for current thread原因及解决方案
在开发中,碰到到了Could not obtain transaction-synchronized Session for current thread异常,因此特意记录下. 一.问 ...
- 关于Could not obtain transaction-synchronized Session for current thread 这个异常。
Could not obtain transaction-synchronized Session for current thread 这个异常之前非常让我头大.对于网上的各种说法都试了一下反正都不 ...
- Could not obtain transaction-synchronized Session for current thread 错误的解决方法!
BsTable bsTable = new BsTable(); // String time = request.getParameter("date"); String tim ...
- [原创]java WEB学习笔记77:Hibernate学习之路---Hibernate 版本 helloword 与 解析,.环境搭建,hibernate.cfg.xml文件及参数说明,持久化类,对象-关系映射文件.hbm.xml,Hibernate API (Configuration 类,SessionFactory 接口,Session 接口,Transaction(事务))
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Hibernate 的Configuration、sessionFactory和session和transaction对象解释
1.Configuration对象: Configuration conf=new Configuration(); conf.configure(); 1.1 到 src下面找到名称hibernat ...
随机推荐
- Flask初级(九)flash与前台交互get详解
Project name :Flask_Plan templates:templates static:static @app.route('/') def hello_world(): return ...
- POJ 2242 The Circumference of the Circle
做题回顾:用到海伦公式,还有注意数据类型,最好统一 p=(a+b+c)/2; s=sqrt(p*(p-a)*(p-b)*(p-c));//三角形面积,海伦公式 r=a*b*c/(4*s);//这是外接 ...
- windowsphone开发页面跳转到另一个dll中的页面
WP的页面跳转一般是只能跳转到本DLL的页面, 如果要跳转到其他DLL的页面则需要这样写 (Application.Current.RootVisual as PhoneApplicationFram ...
- DevExpress v18.1新版亮点——ASP.NET篇(一)
用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress ASP.NET v18.1 的新功能,快来下载试用新版本!点 ...
- 中断一个telnet连接
假如我要telnet一个端口通不通,测试通过之后后出现黑屏的界面 这时候需要按下ctrl+] 组合键 然后输入 quit 即可退出telnet窗口,可以继续测试下一个端口~
- iOS多线程编程:线程同步总结
1:原子操作 - OSAtomic系列函数 iOS平台下的原子操作函数都以OSAtomic开头,使用时需要包含头文件<libkern/OSBase.h>.不同线程如果通过原子操作函数对同一 ...
- 基于PHP+MYSQL的WEB聊天应用雏形开发实例教程
更多技术资源:胡旭个人博客 前天,简单利用PHP做了一个简单的WEB聊天应用.没有发到网上,所以就不提供demo了.不过,发布一下源代码(见文尾). 项目说明: 基于PHP+MYSQL的WEB聊天应用 ...
- css background url 路径
刚刚碰到一个奇怪的问题,这样一段CSS代码: 1 .pho6 { background: url(img/pho6.jpg); } 这段代码居然不能显示出背景图片,路经绝对是没错的代码肯定没有问题, ...
- kbmMW 5.06.20试用笔记
1.kbmMWConfiguration自动备份配置文件的问题还没有修正. 下面是以前写过的内容,再一次在新闻组中提出这个问题: kbmMW提供一个强大的配置信息管理对象,前期译过这个对象的介绍,在使 ...
- GSM中时隙、信道、突发序列、帧的解释
刚从论坛中看到有人问GSM中时隙.信道.突发序列.帧知识.今天我们数字通信正好上到这一块,我就根据我知道的和网上搜索的回答! 1.时分多路复用技术 FDMA:频分多址 TDMA:时分多址 CDMA:码 ...