前言

  • 使用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相关推荐

  1. 关于spring事务传播行为引发的Transaction rolled back because it has been marked as rollback-only

    偶尔博客闲逛发现有人讨论这个问题(我自己没有遇到过),翻了几个帖子没有几个讲清楚的,自己测试下吧 测试类: package com.web.service;import com.StudyApplic ...

  2. 【Spring】21、用spring目标对象处理Transaction rolled back because it has been marked as rollback-only...

    在使用spring做事务管理时,很多人都会遇到这样一段异常: org.springframework.transaction.UnexpectedRollbackException: Transact ...

  3. Spring嵌套事务异常Transaction rolled back because it has been marked as rollback-only

    项目场景: 在循环里面使用try-catch去捕获异常的时候,并且try里面调的方法它也使用了事务注解 @transactional或者用了事务切面AOP去实现方法事务注入 问题描述 我这里的业务需求 ...

  4. Spring事务(Transaction)的传播(propagation)属性以及隔离(isolation)级别

    目录 1 Spring事务 1.1 定义 1.1.1 事务概念 1.1.2 事务分类 1.2 声明式事务 1.2.1 @EnableTransactionManagement工作原理 1.2.2 实现 ...

  5. 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 ...

  6. “Transaction rolled back because it has been marked as rollback-only”

    spring的声明事务提供了强大功能,让我们把业务关注和非业务关注的东西又分离开了.好东西的使用,总是需要有代价的.使用声明事务的时候,一 个不小心经常会碰到"Transaction rol ...

  7. Transaction has been rolled back because it has been marked as rollback

    框架采用的是spring管理声明式事务,这几天业务开发时遇到了点麻烦,记录下备忘. 场景:在Service类中使用子事务(saveponit)时,当子事务抛出异常后,此异常处理掉不继续往外抛,spri ...

  8. 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,事 ...

  9. 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 ...

最新文章

  1. 免高考!2021清华北大保送名单公布,五大学科竞赛260人获保送资格
  2. c语言防止缓冲区数据作为有效字符被读入
  3. 一张图搞定SDF的概念
  4. python已知有列表_python 列表常用方法
  5. do while(false)实用技巧
  6. C/S模型之TCP协议
  7. 高质量linux c编程指南,《linux c编程指南》学习手记5
  8. mybatis 主从表关联查询封装返回结果
  9. C#_MVC 自定义AuthorizeAttribute实现权限管理
  10. kali虚拟机安装教程(超详细)
  11. Linux E: 无法定位软件包
  12. 信息系统项目管理-项目变更管理-十六
  13. 单文件PHP版视频解析源码(中间件)
  14. TS中限制某种类型的传递
  15. 数据挖掘的10大算法
  16. HTTP状态 500 - 内部服务器错误java.lang.NullPointerException
  17. EOJ 3452 唐纳德先生和假骰子
  18. 02 -02 36kr项目
  19. 云展网教程 | 为什么下载下来的EXE文件打不开?
  20. SAP FIORI专题之一:初次接触fiori,用sap hana studio开发发布UI5程序

热门文章

  1. 分布式服务器集群架构方案思考
  2. ASA防火墙外部web应用端口与默认审查协议相冲突的解决方法
  3. 如何使用selenium webdriver来判断一个网页加载完毕
  4. 通过js引用外部脚本(方便直接在浏览器上调试抓取代码)
  5. 架构解密:从分布式到微服务
  6. 【java.lang.UnsupportedClassVersionError】版本不一致出错
  7. ASP.NET MVC 重点教程一周年版 第九回 HtmlHelper
  8. 一些iis配置相关报错的参考
  9. apache密码生成工具htpasswd使用详解
  10. PHP算法为数组补全不存在的键