Spring源码分析 事物

    xiaoxiao2025-08-02  18

    Spring源码分析事物

    Spring事物 源头如下

    public class TransactionInterceptor extends TransactionAspectSupport implements MethodInterceptor, Serializable {

    }

    TransactionInterceptor 事物拦截器。当想要调用目标方法时(如操作数据层),会先转换为代理对象, 回顾之前AOP设计详细阐述过代理对象生命周期,本文不在阐述,代理对象可以访问到TransactionInterceptor, 事物拦截器首先会进行拦截,然后开启事物,接着执行目标方法、最后提交或者回滚事物

    MethodInterceptor是AOP作用的类,里面有抽象方法invoke,详细分析方法如下

    public Object invoke(final MethodInvocation invocation) throws Throwable { final TransactionAttribute txAttr; final PlatformTransactionManager tm; final String joinpointIdentification; TransactionAspectSupport.TransactionInfo txInfo; Object retVal; //1 Class targetClass = invocation.getThis() == null ? null : AopUtils.getTargetClass(invocation.getThis()); //2 txAttr = getTransactionAttributeSource().getTransactionAttribute(invocation.getMethod(), targetClass); //3 tm = determineTransactionManager(txAttr); joinpointIdentification = methodIdentification(invocation.getMethod(), targetClass); if(txAttr != null && (tm instanceof CallbackPreferringPlatformTransactionManager)) break MISSING_BLOCK_LABEL_136; //4 txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification); retVal = null; try { retVal = invocation.proceed(); } catch(Throwable ex) { completeTransactionAfterThrowing(txInfo, ex); throw ex; } cleanupTransactionInfo(txInfo); break MISSING_BLOCK_LABEL_127; Exception exception; exception; cleanupTransactionInfo(txInfo); throw exception; commitTransactionAfterReturning(txInfo); return retVal; try { Object result = ((CallbackPreferringPlatformTransactionManager)tm).execute(txAttr, new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { TransactionAspectSupport.TransactionInfo txInfo = prepareTransactionInfo(tm, txAttr, joinpointIdentification, status); Object obj = invocation.proceed(); cleanupTransactionInfo(txInfo); return obj; Throwable ex; ex; ThrowableHolder throwableholder; if(txAttr.rollbackOn(ex)) if(ex instanceof RuntimeException) throw (RuntimeException)ex; else throw new ThrowableHolderException(ex); throwableholder = new ThrowableHolder(ex); cleanupTransactionInfo(txInfo); return throwableholder; Exception exception1; exception1; cleanupTransactionInfo(txInfo); throw exception1; } final PlatformTransactionManager val$tm; final TransactionAttribute val$txAttr; final String val$joinpointIdentification; final MethodInvocation val$invocation; final TransactionInterceptor this$0; { this$0 = TransactionInterceptor.this; tm = platformtransactionmanager; txAttr = transactionattribute; joinpointIdentification = s; invocation = methodinvocation; super(); } } ); if(result instanceof ThrowableHolder) throw ((ThrowableHolder)result).getThrowable(); else return result; } catch(ThrowableHolderException ex) { throw ex.getCause(); } } protected TransactionInfo createTransactionIfNecessary( PlatformTransactionManager tm, final TransactionAttribute txAttr, String joinpointIdentification) { if (txAttr != null && txAttr.getName() == null) txAttr = new DelegatingTransactionAttribute(joinpointIdentification) { public String getName() { return joinpointIdentification; } final String val$joinpointIdentification; final TransactionAspectSupport this$0; { this$0 = TransactionAspectSupport.this; joinpointIdentification = s; super(x0); } }; TransactionStatus status = null; if (txAttr != null) if (tm != null) //tx1 status = tm.getTransaction(txAttr); else if (logger.isDebugEnabled()) logger.debug((new StringBuilder()) .append("Skipping transactional joinpoint [") .append(joinpointIdentification) .append("] because no transaction manager has been configured") .toString()); return prepareTransactionInfo(tm, txAttr, joinpointIdentification, status); }

    如上//1 获得被代理真正的对象 //2判断方法配置的事物属性是否存在,并获取到目标方法和事物对应的相关属性信息 PlatformTransactionManager 本身是接口是spring事物管理器的抽象,//3获取到事物管理器 createTransactionIfNecessary 创建事物, 重点阐述下,很多情况下配置的事物未生效,tx1会根据事物传播行为获取事物当前状态,如果外层已有事物已开启, 此时隔离级别和超时都会被忽略掉,适应于新创建的事物,最终获得TransactionInfo事物对象 //4 此时事物开启,//5环绕执行目标方法,//6事物回滚 ,//7事物提交

    作者简介:张程 技术研究

    更多文章请关注微信公众号:zachary分解狮 (frankly0423)

    最新回复(0)