关于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 ...
随机推荐
- mobilebone.js-mobile移动web APP单页切换骨架
昨天看到张鑫旭前辈发了一条微博,便互动了下,是他的一个开源移动端框架,没事看看,这是效果
- Webbrowser checkbox
var i:integer; t:OleVariant;begin t := EmbeddedWB1.Document; for i := 0 to t.all.length - 1 do ...
- ansible 删除路径下的多个文件[收集的参考]
How to delete multiple files / directories in Ansible ansible - find, file modules to delete files a ...
- 深入理解Linux网络技术内幕——网络设备初始化
概述 内核的初始化过程过程中,与网络相关的工作如下所示: 内核引导时执行start_kernel,start_kernel结束之前会调用rest_init,rest_init初始化内核线 ...
- react和vue——比较
相同点: 1.JavaScript的UI框架.专注于创造前端的富应用. 2.都有虚拟DOM,DOM树的虚拟表现------改变真实的DOM状态比改变一个JavaScript对象的花销要大得多. Vir ...
- 在linux中使用终端浏览器w3m
w3m是一个基于文本的网页浏览器,支持多种操作系统,在命令行终端可以很好的支持中文.即使在没有鼠标支持的情况下也可以检查网页的输出. 1. 安装w3m $ sudo apt install w3m 2 ...
- PyTorch 数据集类 和 数据加载类 的一些尝试
最近在学习PyTorch, 但是对里面的数据类和数据加载类比较迷糊,可能是封装的太好大部分情况下是不需要有什么自己的操作的,不过偶然遇到一些自己导入的数据时就会遇到一些问题,因此自己对此做了一些小实 ...
- linux makefile (English)
############################################################################## Generic Makefile for ...
- RESTful API 学习
/********************************************************************************* * RESTful API 学习 ...
- vim configures for normal work
" " 主要用于保留一些常用的vim配置,省得每次都另外写,效率太低. " " 2015-09-18 深圳 南山平山村 曾剑锋 " set nocom ...