Spring事务 Transaction rolled back because it has been marked as rollback-only
前言
- 使用Spring事务时,出现异常:
org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only
- 这个错误正确的理解应为:在提交事务时,发现该事务已经回滚了!!!
- 通常该错误发生在事务嵌套时,异常被中途捕获,导致上层的事务提交时失败。
部分错误日志
19:29:37.202 [http-nio-80-exec-11] ERROR c.o.f.w.e.GlobalExceptionHandler - [notFount,64] - 运行时异常:
org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-onlyat org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:873)at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:710)at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:533)at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:304)at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
分析代码
异常抛出的代码片段(org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback
):
/*** Process an actual rollback.* The completed flag has already been checked.* @param status object representing the transaction* @throws TransactionException in case of rollback failure*/private void processRollback(DefaultTransactionStatus status, boolean unexpected) {try {boolean unexpectedRollback = unexpected;try {triggerBeforeCompletion(status);if (status.hasSavepoint()) {if (status.isDebug()) {logger.debug("Rolling back transaction to savepoint");}status.rollbackToHeldSavepoint();}else if (status.isNewTransaction()) {if (status.isDebug()) {logger.debug("Initiating transaction rollback");}doRollback(status);}else {// Participating in larger transactionif (status.hasTransaction()) {if (status.isLocalRollbackOnly() || isGlobalRollbackOnParticipationFailure()) {if (status.isDebug()) {logger.debug("Participating transaction failed - marking existing transaction as rollback-only");}doSetRollbackOnly(status);}else {if (status.isDebug()) {logger.debug("Participating transaction failed - letting transaction originator decide on rollback");}}}else {logger.debug("Should roll back transaction but cannot - no transaction available");}// Unexpected rollback only matters here if we're asked to fail earlyif (!isFailEarlyOnGlobalRollbackOnly()) {unexpectedRollback = false;}}}catch (RuntimeException | Error ex) {triggerAfterCompletion(status, TransactionSynchronization.STATUS_UNKNOWN);throw ex;}triggerAfterCompletion(status, TransactionSynchronization.STATUS_ROLLED_BACK);// Raise UnexpectedRollbackException if we had a global rollback-only markerif (unexpectedRollback) {throw new UnexpectedRollbackException("Transaction rolled back because it has been marked as rollback-only");}}finally {cleanupAfterCompletion(status);}}
通过这段代码,可以看出,出现了非预期的回滚(unexpectedRollback)。
似懂非懂了。
再看看调用processRollback
方法的org.springframework.transaction.support.AbstractPlatformTransactionManager.commit
方法中的代码片段:
/*** This implementation of commit handles participating in existing* transactions and programmatic rollback requests.* Delegates to {@code isRollbackOnly}, {@code doCommit}* and {@code rollback}.* @see org.springframework.transaction.TransactionStatus#isRollbackOnly()* @see #doCommit* @see #rollback*/@Overridepublic final void commit(TransactionStatus status) throws TransactionException {if (status.isCompleted()) {throw new IllegalTransactionStateException("Transaction is already completed - do not call commit or rollback more than once per transaction");}DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status;if (defStatus.isLocalRollbackOnly()) {if (defStatus.isDebug()) {logger.debug("Transactional code has requested rollback");}processRollback(defStatus, false);return;}if (!shouldCommitOnGlobalRollbackOnly() && defStatus.isGlobalRollbackOnly()) {if (defStatus.isDebug()) {logger.debug("Global transaction is marked as rollback-only but transactional code requested commit");}processRollback(defStatus, true);return;}processCommit(defStatus);}
这段代码的中心思想,是要提交事务。但,阴差阳错的没有提交事务。
调用processRollback
前有这样一句话:Global transaction is marked as rollback-only but transactional code requested commit
(全局事务标记为仅回滚,但事务代码请求提交)。
结合上下文理解为:在提交事务时,发现该事务已经回滚了。所以,必须抛出错误。
Spring事务 Transaction rolled back because it has been marked as rollback-only相关推荐
- 关于spring事务传播行为引发的Transaction rolled back because it has been marked as rollback-only
偶尔博客闲逛发现有人讨论这个问题(我自己没有遇到过),翻了几个帖子没有几个讲清楚的,自己测试下吧 测试类: package com.web.service;import com.StudyApplic ...
- 【Spring】21、用spring目标对象处理Transaction rolled back because it has been marked as rollback-only...
在使用spring做事务管理时,很多人都会遇到这样一段异常: org.springframework.transaction.UnexpectedRollbackException: Transact ...
- Spring嵌套事务异常Transaction rolled back because it has been marked as rollback-only
项目场景: 在循环里面使用try-catch去捕获异常的时候,并且try里面调的方法它也使用了事务注解 @transactional或者用了事务切面AOP去实现方法事务注入 问题描述 我这里的业务需求 ...
- Spring事务(Transaction)的传播(propagation)属性以及隔离(isolation)级别
目录 1 Spring事务 1.1 定义 1.1.1 事务概念 1.1.2 事务分类 1.2 声明式事务 1.2.1 @EnableTransactionManagement工作原理 1.2.2 实现 ...
- Transaction rolled back because it has been marked as rollback-only
http://hsyd.iteye.com/blog/586772 错误信息: Transaction rolled back because it has been marked as rollba ...
- “Transaction rolled back because it has been marked as rollback-only”
spring的声明事务提供了强大功能,让我们把业务关注和非业务关注的东西又分离开了.好东西的使用,总是需要有代价的.使用声明事务的时候,一 个不小心经常会碰到"Transaction rol ...
- Transaction has been rolled back because it has been marked as rollback
框架采用的是spring管理声明式事务,这几天业务开发时遇到了点麻烦,记录下备忘. 场景:在Service类中使用子事务(saveponit)时,当子事务抛出异常后,此异常处理掉不继续往外抛,spri ...
- UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only
转载自:https://dongguabai.blog.csdn.net/article/details/114686998 PROPAGATION_REQUIRES_NEW:原有事务A新起事务B,事 ...
- Transaction rolled back because it has been marked as rollback-only分析解决方法
Transaction rolled back because it has been marked as rollback-only分析解决方法 参考文章: (1)Transaction rolle ...
最新文章
- 免高考!2021清华北大保送名单公布,五大学科竞赛260人获保送资格
- c语言防止缓冲区数据作为有效字符被读入
- 一张图搞定SDF的概念
- python已知有列表_python 列表常用方法
- do while(false)实用技巧
- C/S模型之TCP协议
- 高质量linux c编程指南,《linux c编程指南》学习手记5
- mybatis 主从表关联查询封装返回结果
- C#_MVC 自定义AuthorizeAttribute实现权限管理
- kali虚拟机安装教程(超详细)
- Linux E: 无法定位软件包
- 信息系统项目管理-项目变更管理-十六
- 单文件PHP版视频解析源码(中间件)
- TS中限制某种类型的传递
- 数据挖掘的10大算法
- HTTP状态 500 - 内部服务器错误java.lang.NullPointerException
- EOJ 3452 唐纳德先生和假骰子
- 02 -02 36kr项目
- 云展网教程 | 为什么下载下来的EXE文件打不开?
- SAP FIORI专题之一:初次接触fiori,用sap hana studio开发发布UI5程序