关于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 ...
 
随机推荐
- System.Web.Caching
			
System.Web.Caching简单封装类: using System; using System.Collections.Generic; using System.Web.Caching; u ...
 - js相关小技巧
			
1.清空type=file的input文件上传控件的内容,相当于重置.txtFile是该input的id $("#txtFile").val(""); $(&q ...
 - mongo长连接
			
php mongoclient默认都是长连接 mongo close方法可以关闭长连接 redis,mysql 短连接和长连接可选 他们的close方法都不可以关闭长连接
 - python爬虫田康林
			
代码如下 import requestsimport json import time import os from threading import Timer def getHTML(url ...
 - js创建表单并提交
			
1.脚本 Util = { post : function(URL, PARAMS){ //虚拟表单实现post提交 var temp = document.createElement("f ...
 - Python获取路径下所有文件名
			
python 获取当前文件夹下所有文件名 os 模块下有两个函数: os.walk() os.listdir() 1 # -*- coding: utf-8 -*- 2 3 import os 4 ...
 - js方法参数问题
			
大家都知道java是强类型语言,而js是弱类型语言,其实,它们之间的区别不止这一点,还有方法参数及调用问题,参看下例: js中:这里定义了一个query()方法 function query() { ...
 - CentOS 下安装和使用 Docker
			
引言: 在服务器开发过程中,环境部署无疑是及其繁琐的事情,特别是当项目数量和规模达到一定级别之后,在一台新的机器上部署项目环境无疑是极其漫长而痛苦的,那么什么办法能够实现我们的目标:在开发环境的一次配 ...
 - exit和return
			
函数名: exit() 所在头文件:stdlib.h(如果是”VC6.0“的话头文件为:windows.h) 功 能: 关闭所有文件,终止正在执行的进程. exit(1)表示异常退出.这个1是返回给操 ...
 - 文件和文件夹不存在的时候,FileSystemWatcher 监听不到文件的改变?如果递归地监听就可以了
			
当你需要监视文件或文件夹的改变的时候,使用 FileSystemWatcher 便可以完成.不过,FileSystemWatcher 对文件夹的监视要求文件夹必须存在,否则会产生错误“无效路径”. 那 ...