关于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 ...
随机推荐
- 1.strcpy使用注意
void test1() { char string[10]; char *str1 ="0123456789"; strcpy(string,str1); } 上述代码问题? 字 ...
- 跟我一起学习ASP.NET 4.5 MVC4.0(二)
上一篇文章中(跟我一起学习ASP.NET 4.5 MVC4.0(一))我们基础的了解了一下ASP.NET MVC4.0的一些比较简单的改变,主要是想对于MVC3.0来说的.因为这一些列主要是要给ASP ...
- python 抓取request信息,各种cookie,user-agent类的信息,只调试到http可以抓取,https貌似不行。
import pcap # 安装的是pypcap,本博客有安装方法,不过也比较乱,试试吧.import dpktimport socketimport datetime def sniffer(str ...
- L1-032 Left-pad
根据新浪微博上的消息,有一位开发者不满NPM(Node Package Manager)的做法,收回了自己的开源代码,其中包括一个叫left-pad的模块,就是这个模块把javascript里面的Re ...
- 对小波变换中DWT和CWT的理解
转载自 http://blog.sina.com.cn/s/blog_633750d90100hbco.html 连续小波变换的概念.操作.及时间尺度图的显示 最近很多网友问到关于连续小波变换的诸多问 ...
- 一個不錯的免費流程圖制作軟件 Download link
The process flow software you saw yesterday which is a free software, but you need to register. Down ...
- TJson.format() 输出错误的CRLF
下面的JSON串: { "a":"x=\"a,b\"" } 通过下面代码输出,多了CRLF: procedure JsonFormatTes ...
- windows form参数传递过程
三.windows form参数传递过程 在Windows 程序设计中参数的传递,同样也是非常的重要的. 这里主要是通过带有参数的构造函数来实现的, 说明:Form1为主窗体,包含控件:文本框text ...
- python3.6 ubuntu
apt-get install python3.6-dev 安装 pip install mysqlclient 必备
- Docker教程-01.安装docker-ce-18.06
参考文章:http://www.runoob.com/docker/docker-tutorial.html 1.Docker简介 1)Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 ...