SpringMVC4+Hibernate4运行报错Could not obtain transaction-synchronized Session for current thread...
Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for current thread”, 这个错误的原因,网上有很多解决办法, 但具体原因的分析,却没有多少, 这里转载一个原理分析:
SessionFactory的getCurrentSession并不能保证在没有当前Session的情况下会自动创建一个新的,这取决于CurrentSessionContext的实现,SessionFactory将调用CurrentSessionContext的currentSession()方法来获得Session。在Spring中,如果我们在没有配置TransactionManager并且没有事先调用SessionFactory.openSession()的情况直接调用getCurrentSession(),那么程序将抛出“No Session found for current thread”异常。如果配置了TranactionManager并且通过@Transactional或者声明的方式配置的事务边界,那么Spring会在开始事务之前通过AOP的方式为当前线程创建Session,此时调用getCurrentSession()将得到正确结果。
然而,产生以上异常的原因在于Spring提供了自己的CurrentSessionContext实现,如果我们不打算使用Spring,而是自己直接从hibernate.cfg.xml创建SessionFactory,并且为在hibernate.cfg.xml
中设置current_session_context_class为thread,也即使用了ThreadLocalSessionContext,那么我们在调用getCurrentSession()时,如果当前线程没有Session存在,则会创建一个绑定到当前线程。
Hibernate在默认情况下会使用JTASessionContext,Spring提供了自己SpringSessionContext,因此我们不用配置current_session_context_class,当Hibernate与Spring集成时,将使用该SessionContext,故此时调用getCurrentSession()的效果完全依赖于SpringSessionContext的实现。
在没有Spring的情况下使用Hibernate,如果没有在hibernate.cfg.xml中配置current_session_context_class,有没有JTA的话,那么程序将抛出"No CurrentSessionContext configured!"异常。此时的解决办法是在hibernate.cfg.xml中将current_session_context_class配置成thread。
在Spring中使用Hibernate,如果我们配置了TransactionManager,那么我们就不应该调用SessionFactory的openSession()来获得Sessioin,因为这样获得的Session并没有被事务管理。
至于解决的办法,可以采用如下方式:
1. 在spring 配置文件中加入
程序代码
<tx:annotation-driven transaction-manager="transactionManager"/>
并且在处理业务逻辑的类上采用注解
程序代码
@Service
public class CustomerServiceImpl implements CustomerService {
@Transactional
public void saveCustomer(Customer customer) {
customerDaoImpl.saveCustomer(customer);
}
...
}
另外在 hibernate 的配置文件中,也可以增加这样的配置来避免这个错误:
程序代码
<property name="current_session_context_class">thread</property>
需要的可以参考这里的代码,有源代码下载测试:http://www.yihaomen.com/article/java/501.htm
转载于:https://blog.51cto.com/9158947/1837422
SpringMVC4+Hibernate4运行报错Could not obtain transaction-synchronized Session for current thread...相关推荐
- 【python asyncio 运行报错】:raise RuntimeError(‘There is no current event loop in thread %r‘)
代码: # 执行第一个协程程序 asyncio.run(S.crawl_url())select_date = S.select_date() select_keyword = S.select_ke ...
- tensorflow 运行报错 runtime error use a closed session
https://blog.csdn.net/weixin_39390732/article/details/76474294
- SAP WM 自动创建TO单的JOB运行报错 - Enter the storage unit type - 对策
SAP WM 自动创建TO单的JOB运行报错 - Enter the storage unit type - 对策 报错如下: 详细信息: Log for automatic creation of ...
- python3运行报错:TypeError: Object of type ‘type‘ is not JSON serializable解决方法(详细)
python3运行报错:TypeError: Object of type 'type' is not JSON serializable解决方法(详细) 参考文章: (1)python3运行报错:T ...
- appium运行报错java.net.SocketException: socket write error
这个错我调了 快两天一点头绪没有,脚本正常跑没问题,但是就是控制台输出信息报错,没法定位问题在哪.报错如图: 虽然这个报错不影响测试结果,但是本人有强迫症,一定要查出究竟: 我的尝试: 1.那天试验, ...
- tomcat运行报错Failed to start component [StandardEngine[Catalina].StandardHost[localhost].
一.问题 tomcat运行报错Failed to start component [StandardEngine[Catalina].StandardHost[localhost]. 多半情况是找不到 ...
- mpi tcp连接报错_MPI分布式编程 --3.OpenMPI多节点运行报错
1. OpenMPI多节点运行报错问题 问题描述:节点一即host3,通过mpirun调用节点二即host4的mpi程序,报错如下. $ mpirun -np 1 --host host4 ./mai ...
- 【Java】Kryo运行报错:Exception in thread “main“ java.lang.IllegalArgumentException:Class is not registered
练习代码如下: import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.io.Input; import com ...
- 运行报错:java.io.IOException: invalid constant type: 15
为什么80%的码农都做不了架构师?>>> jdk,tomcat更新到jdk1.8与 tomcat8 运行报错:java.io.IOException: invalid cons ...
- RMAN备份恢复报错 ORA-19573: cannot obtain exclusive enqueue for datafile 5
RMAN备份恢复报错 ORA-19573: cannot obtain exclusive enqueue for datafile 5 用vi 编辑了一个数据文件 ,导致下面的错误: 验证还原 RM ...
最新文章
- R语言ggplot2可视化小提琴图(violin plot)并使用ggsignif添加分组显著性(significance)标签
- linux下开放端口
- VSCode配置 Debugger for Chrome插件
- 信息学奥赛一本通(1130:找第一个只出现一次的字符)
- ansj 自定义 停用词_构造自定义停用词列表的快速提示
- ubuntu16.04下怎么安装flash player
- Openvswitch手册(7): Interfaces
- 学习java一般多久
- ADB彻底清除小米电视广告,本人实测
- ssb的matlab仿真,单边带调制(SSB调制)的理论基础和MATLAB仿真
- 2048php,Phaser实现2048
- 第11篇 zephyr 数据传递之LIFO
- 计算机视频剪辑教程,VLOG视频剪辑教程
- 开发岗校招求职攻略——面试准备(7.2胸有成竹-技术面技巧)
- 一种简单而有趣的数据结构——并查集
- multi-angle cosine and sines
- 程序员创业必读的几本书
- 2011考研数学二第(20)题——积分应用:旋转体的体积
- html文件导入excel 出现乱码,win7系统txt导入excel出现中文乱码的解决方法
- 几百套AE模板企业片头动画