Spring 异常捕获后依旧回滚

问题抛出

在 Spring 的声明式事务中手动捕获异常,居然判定回滚了,例如 A 里面调用 B ,C ,C 里面抛出了异常,A 里面对 C 进行了 try catch 依然会回滚,上代码

@EnableTransactionManagement
@SpringBootApplication
public class BingApplication {public static void main(String[] args) {SpringApplication.run(BingApplication.class, args);}
}@Controller
public class IndexController {@AutowiredServiceA serviceA;@RequestMapping("test")@ResponseBodypublic String test() {serviceA.operate();return "OK";}}@Service
public class ServiceA {@Autowiredprivate ServiceB serviceB;@Autowiredprivate ServiceC serviceC;@Transactionalpublic void operate() {serviceB.insertB();try {serviceC.insertC();} catch (Exception e) {e.printStackTrace();}}
}@Service
public class ServiceB {@AutowiredImageMapper imageMapper;@Transactionalpublic void insertB() {Image image = new Image();image.setDescription("descriptionb");image.setDate("dateb");image.setUrl("urlb");imageMapper.insert(image);}
}@Service
public class ServiceC {@AutowiredImageMapper imageMapper;@Transactionalpublic void insertC() {Image image = new Image();image.setDescription("descriptionc");image.setDate("datec");image.setUrl("urlc");imageMapper.insert(image);throw new RuntimeException();}
}

正常情况下,因为我们对 insertC 进行了 try catch,所以我们期望 b 都能插入数据成功,但是都没插入成功,证明被回滚了
@Transactional 默认的传播机制是 Propagation.REQUIRED,REQUIRED 的意思是当前没有事物就创建新的事物,当前有事物
就加入事物。

源码分析

事物的代理入口类是 TransactionInterceptor,该类通过 ProxyTransactionManagementConfiguration 注册到容器中

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public TransactionInterceptor transactionInterceptor(TransactionAttributeSource transactionAttributeSource) {TransactionInterceptor interceptor = new TransactionInterceptor();interceptor.setTransactionAttributeSource(transactionAttributeSource);if (this.txManager != null) {interceptor.setTransactionManager(this.txManager);}return interceptor;
}

里面重要的是 invoke 方法,invoke 方法中重要的是 invokeWithinTransaction 方法

@Nullable
protected Object invokeWithinTransaction(Method method, @Nullable Class<?> targetClass,final InvocationCallback invocation) throws Throwable {// If the transaction attribute is null, the method is non-transactional.TransactionAttributeSource tas = getTransactionAttributeSource();final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null);final TransactionManager tm = determineTransactionManager(txAttr);。。。。。。PlatformTransactionManager ptm = asPlatformTransactionManager(tm);final String joinpointIdentification = methodIdentification(method, targetClass, txAttr);// 注解的事务会走这里if (txAttr == null || !(ptm instanceof CallbackPreferringPlatformTransactionManager)) {// 开启事务// Standard transaction demarcation with getTransaction and commit/rollback calls.TransactionInfo txInfo = createTransactionIfNecessary(ptm, txAttr, joinpointIdentification);Object retVal;try {// This is an around advice: Invoke the next interceptor in the chain.// This will normally result in a target object being invoked.retVal = invocation.proceedWithInvocation();}catch (Throwable ex) {// target invocation exception// 回滚completeTransactionAfterThrowing(txInfo, ex);throw ex;}finally {cleanupTransactionInfo(txInfo);}if (retVal != null && vavrPresent && VavrDelegate.isVavrTry(retVal)) {// Set rollback-only in case of Vavr failure matching our rollback rules...TransactionStatus status = txInfo.getTransactionStatus();if (status != null && txAttr != null) {retVal = VavrDelegate.evaluateTryFailure(retVal, txAttr, status);}}// 提交事务commitTransactionAfterReturning(txInfo);return retVal;}。。。。。。
}

createTransactionIfNecessary 中有个 getTransaction

@Override
public final TransactionStatus getTransaction(@Nullable TransactionDefinition definition)throws TransactionException {// Use defaults if no transaction definition given.TransactionDefinition def = (definition != null ? definition : TransactionDefinition.withDefaults());Object transaction = doGetTransaction();boolean debugEnabled = logger.isDebugEnabled();// 如果存在事务if (isExistingTransaction(transaction)) {// 处理存在事务的情况// Existing transaction found -> check propagation behavior to find out how to behave.return handleExistingTransaction(def, transaction, debugEnabled);}// Check definition settings for new transaction.if (def.getTimeout() < TransactionDefinition.TIMEOUT_DEFAULT) {throw new InvalidTimeoutException("Invalid transaction timeout", def.getTimeout());}// No existing transaction found -> check propagation behavior to find out how to proceed.if (def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_MANDATORY) {throw new IllegalTransactionStateException("No existing transaction found for transaction marked with propagation 'mandatory'");}// 第一次大部分走这里else if (def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED ||def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW ||def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) {SuspendedResourcesHolder suspendedResources = suspend(null);if (debugEnabled) {logger.debug("Creating new transaction with name [" + def.getName() + "]: " + def);}try {return startTransaction(def, transaction, debugEnabled, suspendedResources);}catch (RuntimeException | Error ex) {resume(null, suspendedResources);throw ex;}}else {// Create "empty" transaction: no actual transaction, but potentially synchronization.if (def.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT && logger.isWarnEnabled()) {logger.warn("Custom isolation level specified but no actual transaction initiated; " +"isolation level will effectively be ignored: " + def);}boolean newSynchronization = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);return prepareTransactionStatus(def, null, true, newSynchronization, debugEnabled, null);}
}

我们第一次进来的时候是没有事务的,所以会新建事务 startTransaction

private TransactionStatus startTransaction(TransactionDefinition definition, Object transaction,boolean debugEnabled, @Nullable SuspendedResourcesHolder suspendedResources) {boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);// 开启一个新的事务,这里有的参数 newTransaction  传入的是 trueDefaultTransactionStatus status = newTransactionStatus(definition, transaction, true, newSynchronization, debugEnabled, suspendedResources);doBegin(transaction, definition);prepareSynchronization(status, definition);return status;
}

第二次进来,因为有事务了,就会走到 handleExistingTransaction, 默认的 REQUIRED 就末尾的这一行

return prepareTransactionStatus(definition, transaction, false, newSynchronization, debugEnabled, null);

这里是传入的是 false,最后,我们返回 commitTransactionAfterReturning 方法中,看 commit 方法 中的 processCommit

// 如果有回滚点
if (status.hasSavepoint()) {if (status.isDebug()) {logger.debug("Releasing transaction savepoint");}unexpectedRollback = status.isGlobalRollbackOnly();status.releaseHeldSavepoint();
}
// 如果是新的事务
else if (status.isNewTransaction()) {if (status.isDebug()) {logger.debug("Initiating transaction commit");}unexpectedRollback = status.isGlobalRollbackOnly();doCommit(status);
}
else if (isFailEarlyOnGlobalRollbackOnly()) {unexpectedRollback = status.isGlobalRollbackOnly();
}

因为 newTransaction 不为 true,我们看异常情况下的回滚 在 org.springframework.transaction.support.AbstractPlatformTransactionManager#commit() 方法中的 processRollback
最终会走到 doSetRollbackOnly

doSetRollbackOnly -> DataSourceTransactionObject.setRollbackOnly() -> ResourceHolderSupport.setRollbackOnly()

public void setRollbackOnly() {this.rollbackOnly = true;
}

所以最后我们提交的时候,在 AbstractPlatformTransactionManager#commit(TransactionStatus status) 方法中

if (defStatus.isLocalRollbackOnly()) {if (defStatus.isDebug()) {logger.debug("Transactional code has requested rollback");}processRollback(defStatus, false);return;
}

如果有 rollbackOnly = true ,所以都会回滚

总结和其他

不同的情况

如果 A 有事物,B 有事物,C 事务,在 A 里面调用 B、C,C抛出运行时异常,即使被 try catch,数据不会插入

如果 A 有事物,B 有事物,C 事务,在 A 里面调用 B、C,C抛出编译时异常,即使被 try catch,数据也会插入

运行时异常 编译时异常

比如 RuntimeException 时 运行时异常,IOException 是编译时异常

如果 A 有事物,B 有事物,C 无事务,在 A 里面调用 B、C,C抛出运行时异常,如果不对 C 进行 try catch,数据不会插入

如果 A 有事物,B 有事物,C 无事务,在 A 里面调用 B、C,C抛出运行时异常,如果对 C 进行 try catch,数据都会插入

来源

https://blog.csdn.net/weixin_64314555/article/details/122492760

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/40512.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

地铁车厢火灾3D模拟逃生演习减少了资源损耗和风险

在消防安全领域&#xff0c;为了更好地提升安全实训效果&#xff0c;我们在VR安全培训领域打造了多款消防安全VR模拟实训系统&#xff0c;不仅实现了与现实世界无异的交互操作&#xff0c;更在虚拟空间中超越了现实的限制&#xff0c;模拟出那些现实中难以搭建的复杂场景。 利用…

mysql:部署MySQL 8.0 环境

mysql网址&#xff1a;MySQL 点击 MySQL Community Server 选择合适的版本 选择8.0版本 下载完成&#xff0c;点击mysql-installer-community-8.0.26.0.msi文件&#xff0c;打开安装向导。 选择自定义安装类型 打开“Select Products” 窗口&#xff0c;可以定制需要安装的产…

成都晨持绪:开一家抖音网店到底能不能赚钱

在数字化时代的浪潮中&#xff0c;抖音以其独特的魅力迅速占领了社交媒体的舞台。众多创业者纷纷把目光投向这个新兴平台&#xff0c;企图在短视频的海洋里找到属于自己的财富岛屿。但是&#xff0c;开一家抖音网店到底能不能赚钱呢? 我们要认识到&#xff0c;抖音作为一个流量…

lt6911UXC 国产原装 高性能HDMI2.0转MIPI DSI / CSI芯片方案 提供LT 开发资料包及在线软硬件技术支持!

1.说明 LT6911UXC是一款高性能HDMI2.0到MIPI DSI / CSI转换器&#xff0c;用于VR&#xff0c;智能电话&#xff0c;显示应用。 HDMI2.0输入支持高达6Gbps的数据速率&#xff0c;从而为4k 60Hz视频提供足够的带宽。还支持HDCP2.2进行数据解密。 对于MIPI DSI / CSI输出&#xf…

【路由交换技术】Cisco Packet Tracer基础入门教程(五)

这一期我们来学习端口聚合&#xff0c;这是针对交换机的技术 前言 不知道大家有没有注意到&#xff0c;我们之前的实验在交换机与交换机之间只用一条线连接&#xff0c;像这样 通过今天的学习&#xff0c;我们要用两条线来连接交换机&#xff0c;就像这样&#xff08;为了能…

人力资源中的人工智能:你应该知道的一切

人工智能已经成为行业讨论更广泛的突出话题。人力资源(HR)对于人力资源专业人士来说&#xff0c;了解这门课程也是如此。除了简要介绍什么是人工智能&#xff0c;以及你可能遇到的主要人工智能类型(或者你可能很快就会遇到它&#xff01;)此外&#xff0c;本文还将探讨人工智能…

10 个实用的 Python 使用技巧

Python 是一门功能强大且易于学习的编程语言&#xff0c;广泛应用于数据科学、机器学习、Web 开发等领域。本文将介绍 10 个实用的 Python 使用技巧&#xff0c;帮助你提升编程效率。 1. 列表生成式 列表生成式&#xff08;List Comprehension&#xff09;是一种简洁的创建列…

教程:Spring Boot中集成Memcached的详细步骤

教程&#xff1a;Spring Boot中集成Memcached的详细步骤 大家好&#xff0c;我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编&#xff0c;也是冬天不穿秋裤&#xff0c;天冷也要风度的程序猿&#xff01; 在现代应用开发中&#xff0c;缓存是提升性能和扩展性…

并发控制-事务的调度、数据不一致问题(更新丢失、脏读、不可重复读)、非串行调度的的可串行化

一、引言 1、数据库管理系统DBMS的事务处理技术实现的另一个主要功能部分是并发控制机制。并发控制机制完成的功能就是对并发执行的事务进行控制&#xff0c;保证事务的隔离性&#xff0c;从而进一步保持数据库的一致性。 2、事务的并发控制就是对并发执行的不同事务中的数据…

利用Python破解隔壁家的WiFi密码

文章目录 1&#xff0c;破解思路1.1&#xff0c;假定邻居家的WiFi密码长度是8位&#xff0c;且仅由0-9这10个数字&#xff0c;a-z这26个小写字母&#xff0c;A-Z这26个大写字母组成1.2&#xff0c; 用python程序对0-9&#xff0c;a-z&#xff0c;A-Z这62个元素进行排列组合。1.…

Java基础:爬虫

1.本地爬虫 Pattern:表示正则表达式 Matcher:文本匹配器&#xff0c;作用按照正则表达式的规则去读取字符串&#xff0c;从头开始读取。在大串中去找符合匹配规则的子串。 1.2.获取Pattern对象 通过Pattern p Pattern.compile("正则表达式");获得 1.3.…

从零开始:手把手教你部署 Node.js 项目

1. 本地项目打包 关闭本地项目后&#xff0c;在控制台执行以下命令进行打包&#xff1a; npm run build2. 将文件拷贝至服务器 将以下文件和文件夹拷贝至服务器&#xff1a; .nuxt 文件夹config 文件夹io 文件夹static 文件夹environment.js 文件nuxt.config.js 文件package.…

Electron:构建跨平台桌面应用的现代方法

环境搭建与Hello World main.js (主进程) const { app, BrowserWindow } require(electron)function createWindow () {// 创建一个新的浏览器窗口const mainWindow new BrowserWindow({width: 800, // 窗口宽度height: 600, // 窗口高度webPreferences: {nodeIntegration:…

【安全攻防】网络安全中的序列化与反序列

1.序列化与反序列化 首先要了解序列化与反序列化的定义&#xff0c;以及序列化反序列化所用到的基本函数。 序列化&#xff1a;把对象转换为字节序列的过程称为对象的序列化&#xff0c;相当于游戏中的存档。 PHP中的序列化函数serialize() **serialize()**函数用于序列化对…

VScode使用ssh连接服务器

VScode是一款有丰富插件的编译器&#xff0c;非常好用&#xff01;除非你不会用&#xff0c;因为太过繁琐或着频繁出错导致想把电脑砸了&#xff1b; 插件选择 ssh 配置文件 Host myblablaHostName xxx.xx.xxx.xxxUser username用户名一般是服务器上创建有什么用户名&#xf…

【Vue】使用html、css实现鱼骨组件

文章目录 组件测试案例预览图 组件 <template><div class"context"><div class"top"><div class"label-context"><div class"label" v-for"(item, index) in value" :key"index">…

ubuntu22sshd服务保持连接的设置

专栏总目录 一、服务端 &#xff08;一&#xff09;打开配置文件 vi /etc/ssh/sshd_config &#xff08;二&#xff09;更改选项 将相关选项配置如下 ClientAliveInterval 60 ClientAliveCountMax 0 TCPKeepAlive yes 修改完毕后&#xff0c;依次点击esc按键、w、q按键回车…

深入探讨JavaScript中的队列,结合leetcode全面解读

前言 队列作为一种基本的数据结构&#xff0c;为解决许多实际问题提供了有效的组织和处理方式&#xff0c;对于提高系统的稳定性、可靠性和效率具有重要作用&#xff0c;所以理解队列是很重要的。 本文深入探讨JavaScript中的队列这种数据结构,结合leetcode题目讲解 题目直达…

在线快速升级nginx

备份现有配置文件和二进制文件&#xff1a; #压缩备份 sudo tar -czvf /usr/sbin/nginx1.21.4_backup.tar.gz /usr/sbin/nginx1.21.4/sudo cp /usr/sbin/nginx1.21.4/conf/nginx.conf /usr/sbin/nginx1.21.4/conf/nginx.conf.bak sudo cp /usr/sbin/nginx1.21.4/sbin/nginx /us…

一、Prometheus和Grafana搭建

一、服务端Prometheus二进制安装 https://prometheus.io/下载过慢可使用迅雷下载 tar -zxvf prometheus-2.53.0.linux-amd64.tar.gz启动 ./prometheus --config.fileprometheus.yml将其配置为系统服务&#xff1a; vim /usr/lib/systemd/system/prometheus.service[Unit] D…