Spring 5.x 源码之旅-67深入AOP事务原理四

作者简介:大家好,我是smart哥,前中兴通讯、美团架构师,现某互联网公司CTO

联系qq:184480602,加我进群,大家一起学习,一起进步,一起对抗互联网寒冬

学习必须往深处挖,挖的越深,基础越扎实!

阶段1、深入多线程

阶段2、深入多线程设计模式

阶段3、深入juc源码解析


阶段4、深入jdk其余源码解析


阶段5、深入jvm源码解析

 

码哥源码部分

码哥讲源码-原理源码篇【2024年最新大厂关于线程池使用的场景题】

码哥讲源码【炸雷啦!炸雷啦!黄光头他终于跑路啦!】

码哥讲源码-【jvm课程前置知识及c/c++调试环境搭建】

 

​​​​​​码哥讲源码-原理源码篇【揭秘join方法的唤醒本质上决定于jvm的底层析构函数】

码哥源码-原理源码篇【Doug Lea为什么要将成员变量赋值给局部变量后再操作?】

码哥讲源码【你水不是你的错,但是你胡说八道就是你不对了!】

码哥讲源码【谁再说Spring不支持多线程事务,你给我抽他!】

终结B站没人能讲清楚红黑树的历史,不服等你来踢馆!

打脸系列【020-3小时讲解MESI协议和volatile之间的关系,那些将x86下的验证结果当作最终结果的水货们请闭嘴】

 

创建事务信息流程图

处理回滚流程图

传播机制图

AbstractPlatformTransactionManager的prepareSynchronization同步状态

设置各种线程私有变量的状态。

    	protected void prepareSynchronization(DefaultTransactionStatus status, TransactionDefinition definition) {
    		if (status.isNewSynchronization()) {
    			TransactionSynchronizationManager.setActualTransactionActive(status.hasTransaction());
    			TransactionSynchronizationManager.setCurrentTransactionIsolationLevel(
    					definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT ?
    							definition.getIsolationLevel() : null);
    			TransactionSynchronizationManager.setCurrentTransactionReadOnly(definition.isReadOnly());
    			TransactionSynchronizationManager.setCurrentTransactionName(definition.getName());
    			TransactionSynchronizationManager.initSynchronization();
    		}
    	}

都是当前事务相关的信息。

TransactionAspectSupport的prepareTransactionInfo准备事务信息

创建了一个TransactionInfo ,然后把事务管理器,事务注解属性,方法标识符,事务状态设置进去。然后绑定到当前线程私有变量里。

    protected TransactionInfo prepareTransactionInfo(@Nullable PlatformTransactionManager tm,
    			@Nullable TransactionAttribute txAttr, String joinpointIdentification,
    			@Nullable TransactionStatus status) {
    		//创建事务信息
    		TransactionInfo txInfo = new TransactionInfo(tm, txAttr, joinpointIdentification);
    		if (txAttr != null) {
    		...
    			txInfo.newTransactionStatus(status);//设置新事务状态
    		}
    		else {
    			
    			if (logger.isTraceEnabled()) {
    				logger.trace("No need to create transaction for [" + joinpointIdentification +
    						"]: This method is not transactional.");
    			}
    		}
    		...
    		txInfo.bindToThread();//事务信息绑定到当前线程
    		return txInfo;
    	}

TransactionAspectSupport的completeTransactionAfterThrowing处理异常

如果支持回滚的话就进行回滚,否则就处理提交,提交里面如果TransactionStatus.isRollbackOnly()=true的话也会进行回滚处理。

    	protected void completeTransactionAfterThrowing(@Nullable TransactionInfo txInfo, Throwable ex) {
    		if (txInfo != null && txInfo.getTransactionStatus() != null) {
    			if (logger.isTraceEnabled()) {
    				logger.trace("Completing transaction for [" + txInfo.getJoinpointIdentification() +
    						"] after exception: " + ex);
    			}
    			if (txInfo.transactionAttribute != null && txInfo.transactionAttribute.rollbackOn(ex)) {
    				try {//进行回滚
    					txInfo.getTransactionManager().rollback(txInfo.getTransactionStatus());
    				}
    				catch (TransactionSystemException ex2) {
    					logger.error("Application exception overridden by rollback exception", ex);
    					ex2.initApplicationException(ex);
    					throw ex2;
    				}
    				catch (RuntimeException | Error ex2) {
    					logger.error("Application exception overridden by rollback exception", ex);
    					throw ex2;
    				}
    			}
    			else {
    
    				try {
    					txInfo.getTransactionManager().commit(txInfo.getTransactionStatus());
    				}
    				catch (TransactionSystemException ex2) {
    					logger.error("Application exception overridden by commit exception", ex);
    					ex2.initApplicationException(ex);
    					throw ex2;
    				}
    				catch (RuntimeException | Error ex2) {
    					logger.error("Application exception overridden by commit exception", ex);
    					throw ex2;
    				}
    			}
    		}
    	}

DelegatingTransactionAttribute的rollbackOn是否可以回滚

回滚对异常是有要求的。

    	@Override
    	public boolean rollbackOn(Throwable ex) {
    		return this.targetAttribute.rollbackOn(ex);
    	}

RuleBasedTransactionAttribute的rollbackOn

先处理回滚的规则,就是事务注解里的rollbackFor,rollbackForClassName,noRollbackFor,noRollbackForClassName属性。如果没有设置就会调用父类的rollbackOn

    @Override
    	public boolean rollbackOn(Throwable ex) {
    		if (logger.isTraceEnabled()) {
    			logger.trace("Applying rules to determine whether transaction should rollback on " + ex);
    		}
    
    		RollbackRuleAttribute winner = null;
    		int deepest = Integer.MAX_VALUE;
    		//处理设置的回滚规则
    		if (this.rollbackRules != null) {
    			for (RollbackRuleAttribute rule : this.rollbackRules) {
    				int depth = rule.getDepth(ex);
    				if (depth >= 0 && depth < deepest) {
    					deepest = depth;
    					winner = rule;
    				}
    			}
    		}
    
    		if (logger.isTraceEnabled()) {
    			logger.trace("Winning rollback rule is: " + winner);
    		}
    
    		// User superclass behavior (rollback on unchecked) if no rule matches.
    		if (winner == null) {
    			logger.trace("No relevant rollback rule found: applying default rules");
    			return super.rollbackOn(ex);
    		}
    
    		return !(winner instanceof NoRollbackRuleAttribute);
    	}
DefaultTransactionAttribute的rollbackOn

可以看到这里就是的异常,要么是RuntimeException的实例,要么是Error的实例。

    	@Override
    	public boolean rollbackOn(Throwable ex) {
    		return (ex instanceof RuntimeException || ex instanceof Error);
    	}

AbstractPlatformTransactionManager的rollback回滚

事务管理器根据事务状态来处理回滚。

    	@Override
    	public final void rollback(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;
    		processRollback(defStatus, false);
    	}

关键的就是processRollback,下次讲。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值