SpringAOP——事务实现细节
Posted FFStayF
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringAOP——事务实现细节相关的知识,希望对你有一定的参考价值。
承接上文,<tx:annotation-driven />开启声明式事务时,在SpringIOC容器中初始化了4个Bean
<!-- 事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 启用声明式事务管理 支持注解@Transaction--> <tx:annotation-driven proxy-target-class="false" transaction-manager="transactionManager"/>
TransactionalEventListenerFactory AnnotationTransactionAttributeSource TransactionInterceptor//SpringAOP方法执行时的责任链拦截器 BeanFactoryTransactionAttributeSourceAdvisor//直接用bean创建的Advisor
由于前面没有具体深入了解每个Bean的作用以及实现,所以面试被难住了,补充一下Spring事务的具体实现。弄清楚三点:
- 每个组件的作用
- 事务的实现流程
- 与Mybatis的对接实现
一、组件初始化
/* org.springframework.transaction.config.AnnotationDrivenBeanDefinitionParser#registerTransactionalEventListenerFactory */ private void registerTransactionalEventListenerFactory(ParserContext parserContext) { //创建TransactionEvenListenerFactory的Beandefinition RootBeanDefinition def = new RootBeanDefinition(); def.setBeanClass(TransactionalEventListenerFactory.class); parserContext.registerBeanComponent(new BeanComponentDefinition(def, TransactionManagementConfigUtils.TRANSACTIONAL_EVENT_LISTENER_FACTORY_BEAN_NAME)); } /* org.springframework.transaction.config.AnnotationDrivenBeanDefinitionParser.AopAutoProxyConfigurer#configureAutoProxyCreator */ public static void configureAutoProxyCreator(Element element, ParserContext parserContext) { //若未注册,先注册SpringAOP相关Beandefinition AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(parserContext, element); String txAdvisorBeanName = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME; if (!parserContext.getRegistry().containsBeanDefinition(txAdvisorBeanName)) { Object eleSource = parserContext.extractSource(element); //Source的Beandefinition //beanName = org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0 RootBeanDefinition sourceDef = new RootBeanDefinition("org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"); //实际eleSource == null sourceDef.setSource(eleSource); sourceDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); //sourceName == org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0 String sourceName = parserContext.getReaderContext().registerWithGeneratedName(sourceDef); //Inteceptor的BeanDefinition //beanName=org.springframework.transaction.interceptor.TransactionInterceptor#0 RootBeanDefinition interceptorDef = new RootBeanDefinition(TransactionInterceptor.class); interceptorDef.setSource(eleSource); interceptorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); //初始化时的重要信息,设置TransactionInterceptor的属性trancationManagerBeanName的beanName,初始化时依赖注入 //实际获取<tx:annotation-driven transaction-manager="" >中的transaction-manage属性,默认值:beanName = transactionManager registerTransactionManager(element, interceptorDef); //TransactionInterception.trancationAttributeSource = AnnotationTransactionAttributeSource interceptorDef.getPropertyValues().add("transactionAttributeSource", new RuntimeBeanReference(sourceName)); //interceptorName = org.springframework.transaction.interceptor.TransactionInterceptor#0 String interceptorName = parserContext.getReaderContext().registerWithGeneratedName(interceptorDef); //advisor的BeanDefinition //beanName = org.springframework.transaction.config.internalTransactionAdvisor RootBeanDefinition advisorDef = new RootBeanDefinition(BeanFactoryTransactionAttributeSourceAdvisor.class); advisorDef.setSource(eleSource); advisorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); //设置两个属性 //advisor.transactionAttributeSource = AnnotationTransactionAttributeSource的beanName //advisor.adviceBeanName = TransactionInterceptor的beanName advisorDef.getPropertyValues().add("transactionAttributeSource", new RuntimeBeanReference(sourceName)); advisorDef.getPropertyValues().add("adviceBeanName", interceptorName); if (element.hasAttribute("order")) { advisorDef.getPropertyValues().add("order", element.getAttribute("order")); } //将组件的BeanDefinition注册到DefaultListableBeanFactory中 parserContext.getRegistry().registerBeanDefinition(txAdvisorBeanName, advisorDef); CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), eleSource); compositeDef.addNestedComponent(new BeanComponentDefinition(sourceDef, sourceName)); compositeDef.addNestedComponent(new BeanComponentDefinition(interceptorDef, interceptorName)); compositeDef.addNestedComponent(new BeanComponentDefinition(advisorDef, txAdvisorBeanName)); parserContext.registerComponent(compositeDef); } }
总结下组件的依赖关系:
//Inteceptor TransactionInterceptor.trancationManagerBeanName = "transactionManager" TransactionInterceptor.transactionAttributeSource = AnnotationTransactionAttributeSource //advisor advisor.transactionAttributeSource = AnnotationTransactionAttributeSource advisor.advice = TransactionInterceptor
二、事务的AOP实现
SpringAOP实现流程:
① <aop:aspectj-autoproxy />开启SpringAOP代理支持后,会初始化AOP相关组件AnnotationAwareAspectJAutoProxyCreator
② 所有的Bean在初始化的最后阶段都会调用AnnotationAwareAspectJAutoProxyCreator.postprocessAfterInitialzation,判断是否需要生成代理类
③ List<Advisor> candidateAdvisors = findCandidateAdvisors();查找所有Advisor,
④ List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);Bean是否是切面关注点的判断,从所有advisor保留对应的advisor,
⑤ advisor不为空则会创建代理实例proxy object,为空不创建代理,直接返回原实例 target object
⑥ 创建一个ProxyFactory实例记录advisor,将ProxyFactory放入到InvocationHandler实例(jdk动态代理对应JdkDynamicAopProxy)中
⑦ 方法调用时,通过动态代理实现,调用代理类的方法,实际是调用InvocationHandler.invoke()方法(jdk动态代理对应JDKDynamicAopProxy.invoke)
⑧ invocationHandler.invoke具体是实现是:从proxyFactory中找到对应的advisor,然后调用advisor.getAdvice获取具体的advice操作侯然先执行advice后执行target object的方法
事务的实现流程是基于SpringAOP实现的。
对应流程③查找所有的advisor,会找到BeanFactoryTransactionAttributeSourceAdvisor
对应流程④Bean是否是切面关注点,根据BeanFactoryTransactionAttributeSourceAdvisor中的Pointcut判断,实际判断是否被@Transaction注解修饰
/* org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor#getPointcut */ /* * BeanFactoryTransactionAttributeSourceAdvisor中的pointCut初始化 */ public Pointcut getPointcut() { return this.pointcut; } //pointCut实际是一个TransactionAttributeSourcePointcut实例 private final TransactionAttributeSourcePointcut pointcut = new TransactionAttributeSourcePointcut() { @Override @Nullable protected TransactionAttributeSource getTransactionAttributeSource() { return transactionAttributeSource; } }; protected TransactionAttributeSourcePointcut() { setClassFilter(new TransactionAttributeSourceClassFilter()); } private class TransactionAttributeSourceClassFilter implements ClassFilter { //判断Bean是否是poincut的一个join point(切面关注点) @Override public boolean matches(Class<?> clazz) { if (TransactionalProxy.class.isAssignableFrom(clazz) || PlatformTransactionManager.class.isAssignableFrom(clazz) || PersistenceExceptionTranslator.class.isAssignableFrom(clazz)) { //三种基础类型直接返回false return false; } //这里tas就是上面设置的AnnotationTrancationAttributeSource TransactionAttributeSource tas = getTransactionAttributeSource(); return (tas == null || tas.isCandidateClass(clazz)); } }
AnnotationTrancationAttributeSource.isCandidateClass():
/* org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#isCandidateClass */ public boolean isCandidateClass(Class<?> targetClass) { for (TransactionAnnotationParser parser : this.annotationParsers) { if (parser.isCandidateClass(targetClass)) { return true; } } return false; } //annotationParsers初始化 //annotationParsers.add(new SpringTransactionAnnotationParser()) //系统支持jta12时annotationParsers.add(new SpringTransactionAnnotationParser()) //系统支持ejb3时this.annotationParsers.add(new Ejb3TransactionAnnotationParser()); public AnnotationTransactionAttributeSource(boolean publicMethodsOnly) { this.publicMethodsOnly = publicMethodsOnly; if (jta12Present || ejb3Present) { this.annotationParsers = new LinkedHashSet<>(4); this.annotationParsers.add(new SpringTransactionAnnotationParser()); if (jta12Present) { this.annotationParsers.add(new JtaTransactionAnnotationParser()); } if (ejb3Present) { this.annotationParsers.add(new Ejb3TransactionAnnotationParser()); } } else { this.annotationParsers = Collections.singleton(new SpringTransactionAnnotationParser()); } } //默认的SpringTransactionAnnotationParse.isCandidateClass //判断类中包含@Transactional注解, public boolean isCandidateClass(Class<?> targetClass) { return AnnotationUtils.isCandidateClass(targetClass, Transactional.class); }
对应流程⑤ 被@Transactional注解修饰的类的Bean,advisor = BeanFactoryTransactionAttributeSourceAdvisor,不为空创建代理实例
对应流程⑥ 将advisor放入到创建ProxFactory实例的advisors容器中
对应流程⑦ ⑧方法调用时获取BeanFactoryTransactionAttributeSourceAdvisor.getAdvice(),先执行advice.invoke,再执行target object.invoke。即开启事务执行sql提交(失败回退)。这里BeanFactoryTransactionAttributeSourceAdvisor.getAdvice()=TransactionInterceptor
三、事务的执行(创建、提交、回滚)
TransactionInterceptor.invoke()
/* org.springframework.transaction.interceptor.TransactionInterceptor#invoke */ public Object invoke(MethodInvocation invocation) throws Throwable { // target object可能为null,所以这里获取targetClass //如果存在targetObject时 targetClass = targetObject.getClass //如果不存在targetObject时 targetClass = null Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null); // 事务执行 // 第三个参数lamda表达式:invocation.proceed() return invokeWithinTransaction(invocation.getMethod(), targetClass, invocation::proceed); }
TransactionAspectSupport.invokeWithinTransaction()
protected Object invokeWithinTransaction(Method method, @Nullable Class<?> targetClass, final InvocationCallback invocation) throws Throwable { //获取初始化时的AnnotationTransactionAttributeSource TransactionAttributeSource tas = getTransactionAttributeSource(); //① 获取事务属性==>@Transactional注解中的属性,生成一个new RuleBasedTransactionAttribute() //方法、类都有@Transactional时上优先级:方法 > 类 //TransactionAttribute继承TransactionDefinition,定义事务属性的实例(与事务的关系,类似于BeanDefinition与Bean) final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null); //② 获取事务管理器, // 先从@Transactional注解中value属性获取, // 若value未设置,则<tx:annotationDriven/>中transaction-manager属性获取 final TransactionManager tm = determineTransactionManager(txAttr); //③ transactionManager的两个分类 //反应式事务管理器,顶级接口ReactiveTransactionManager,例如MongoDB //响应式事务管理器,顶级接口PlatformTransactionManager,常用的关系型数据库mysql等 if (this.reactiveAdapterRegistry != null && tm instanceof ReactiveTransactionManager) { //反应式事务管理器,是spring5.2之后加入的新支持,反正用到这个东西的公司我也进不了,直接跳过吧 ReactiveTransactionSupport txSupport = this.transactionSupportCache.computeIfAbsent(method, key -> { if (KotlinDetector.isKotlinType(method.getDeclaringClass()) && KotlinDelegate.isSuspend(method)) { throw new TransactionUsageException( "Unsupported annotated transaction on suspending function detected: " + method + ". Use TransactionalOperator.transactional extensions instead."); } ReactiveAdapter adapter = this.reactiveAdapterRegistry.getAdapter(method.getReturnType()); if (adapter == null) { throw new IllegalStateException("Cannot apply reactive transaction to non-reactive return type: " + method.getReturnType()); } return new ReactiveTransactionSupport(adapter); }); return txSupport.invokeWithinTransaction( method, targetClass, invocation, txAttr, (ReactiveTransactionManager) tm); } //响应式事务管理器实现 //如果transactionManager是PlatforTransactionManager的实现类,类型强转;如果不是,抛出异常 PlatformTransactionManager ptm = asPlatformTransactionManager(tm); //获取方法的id作为事务的名称 //joinpointIdentification = method.getDeclaringClass()).getName() + \'.\' + method.getName() final String joinpointIdentification = methodIdentification(method, targetClass, txAttr); if (txAttr == null || !(ptm instanceof CallbackPreferringPlatformTransactionManager)) { //④ 非CallBackPreferringPlatformTransactionManager的TransactionManager的事务管理 // ⑤ 开启事务 TransactionInfo txInfo = createTransactionIfNecessary(ptm, txAttr, joinpointIdentification); Object retVal; try { // 事务的advice是一个 around advice //⑥执行方法(执行sql) retVal = invocation.proceedWithInvocation(); } catch (Throwable ex) { // target invocation exception //⑦ 异常回滚 completeTransactionAfterThrowing(txInfo, ex); throw ex; } finally { //⑧ 清理资源,ThreadLocal中的TransactionInfo cleanupTransactionInfo(txInfo); } if (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; } else { //⑩ CallbackPreferringPlatformTransactionManager final ThrowableHolder throwableHolder = new ThrowableHolder(); // It\'s a CallbackPreferringPlatformTransactionManager: pass a TransactionCallback in. try { Object result = ((CallbackPreferringPlatformTransactionManager) ptm).execute(txAttr, status -> { TransactionInfo txInfo = prepareTransactionInfo(ptm, txAttr, joinpointIdentification, status); try { Object retVal = invocation.proceedWithInvocation(); if (vavrPresent && VavrDelegate.isVavrTry(retVal)) { // Set rollback-only in case of Vavr failure matching our rollback rules... retVal = VavrDelegate.evaluateTryFailure(retVal, txAttr, status); } return retVal; } catch (Throwable ex) { if (txAttr.rollbackOn(ex)) { // A RuntimeException: will lead to a rollback. if (ex instanceof RuntimeException) { throw (RuntimeException) ex; } else { throw new ThrowableHolderException(ex); } } else { // A normal return value: will lead to a commit. throwableHolder.throwable = ex; return null; } } finally { cleanupTransactionInfo(txInfo); } }); // Check result state: It might indicate a Throwable to rethrow. if (throwableHolder.throwable != null) { throw throwableHolder.throwable; } return result; } catch (ThrowableHolderException ex) { throw ex.getCause(); } catch (TransactionSystemException ex2) { if (throwableHolder.throwable != null) { logger.error("Application exception overridden by commit exception", throwableHolder.throwable); ex2.initApplicationException(throwableHolder.throwable); } throw ex2; } catch (Throwable ex2) { if (throwableHolder.throwable != null) { logger.error("Application exception overridden by commit exception", throwableHolder.throwable); } throw ex2; } } }
1、事务创建——①~⑤
① final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null) : 创建一个TransactionDefinition实例
根据@Transactional注解,创建一个TransactionDefinition实例(事务的属性定义实例,与事务的关系类似于Bean与BeanDefinition)实际类型RuleBasedTransactionAttribute
方法、类都有@Transactional注解时,优先级 :方法 > 类
/* org.springframework.transaction.interceptor.AbstractFallbackTransactionAttributeSource#getTransactionAttribute */ /* * 缓存机制 */ public TransactionAttribute getTransactionAttribute(Method method, @Nullable Class<?> targetClass) { if (method.getDeclaringClass() == Object.class) { //Object的方法,返回null return null; } // 缓存机制 Object cacheKey = getCacheKey(method, targetClass); TransactionAttribute cached = this.attributeCache.get(cacheKey); if (cached != null) { // Value will either be canonical value indicating there is no transaction attribute, // or an actual transaction attribute. if (cached == NULL_TRANSACTION_ATTRIBUTE) { //缓存是没有事务属性的单例,返回null return null; } else { return cached; } } else { // 没有缓存时,根据@Transactional注解生成事务属性 TransactionAttribute txAttr = computeTransactionAttribute(method, targetClass); // 缓存处理 if (txAttr == null) { this.attributeCache.put(cacheKey, NULL_TRANSACTION_ATTRIBUTE); } else { String methodIdentification = ClassUtils.getQualifiedMethodName(method, targetClass); if (txAttr instanceof DefaultTransactionAttribute) { //设置事务描述,后面其实以这个属性作为事务的名称 //transactionName = desciptor = method.getDeclaringClass().getName() + "." + method.getName() ((DefaultTransactionAttribute) txAttr).setDescriptor(methodIdentification); } if (logger.isTraceEnabled()) { logger.trace("Adding transactional method \'" + methodIdentification + "\' with attribute: " + txAttr); } this.attributeCache.put(cacheKey, txAttr); } return txAttr; } } /* * 由于代理类的存在,所以需要先找到具体被@Transaction注解的method(targetClass、superClass甚至interface上),然后解析@Transaction生成TransactionDefinition * @Transactional注解的优先级:当同时注解方法和类时,方法 > 类 */ protected TransactionAttribute computeTransactionAttribute(Method method, @Nullable Class<?> targetClass) { if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) { //默认publicMethodsOnly == true,所以如果方法不是public修饰的,返回null return null; } //targetClass == null,specificMethod = method //targetClass != null, // 方法在targetClass声明或重写, // Jdk动态代理 specificMethod = method,CgLib动态代理specificMethod = superClass.method // 方法在不再targetClass中声明且重写, // public方法,specificMethod = method // 非public方法,specificMethod = superClass.method或者接口中的method Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass); // 如果方法被@Transactional注解,解析方法上的@Transactional生成事务属性实例 TransactionAttribute txAttr = findTransactionAttribute(specificMethod); if (txAttr != null) { return txAttr; } // 如果方法上没有@Transaction注解, // 解析方法的声明类(可能是targetClass,superClass,甚至interface)上的@Transaction生成事务属性实例 txAttr = findTransactionAttribute(specificMethod.getDeclaringClass()); if (txAttr != null && ClassUtils.isUserLevelMethod(method)) { return txAttr; } //如果上面txAttr都为空, if (specificMethod != method) { // 代理实例的method方法上的@Transactional注解,AspectJ txAttr = findTransactionAttribute(method); if (txAttr != null) { return txAttr; } // 代理方法的Class类上的@Transactional注解,AspectJ txAttr = findTransactionAttribute(method.getDeclaringClass()); if (txAttr != null && ClassUtils.isUserLevelMethod(method)) { return txAttr; } } return null; } /*org.springframework.transaction.annotation.SpringTransactionAnnotationParser#parseTransactionAnnotation(java.lang.reflect.AnnotatedElement) */ public TransactionAttribute parseTransactionAnnotation(AnnotatedElement element) { AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes( element, Transactional.class, false, false); if (attributes != null) { return parseTransactionAnnotation(attributes); } else { return null; } } /* * 解析@Transactional生成一个TransactionDefinition实例(事务属性实例),实际类型:RuleBasedTransactionAttribute */ protected TransactionAttribute parseTransactionAnnotation(AnnotationAttributes attributes) { RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute(); Propagation propagation = attributes.getEnum("propagation"); rbta.setPropagationBehavior(propagation.value()); Isolation isolation = attributes.getEnum("isolation"); rbta.setIsolationLevel(isolation.value()); rbta.setTimeout(attributes.getNumber("timeout").intValue()); rbta.setReadOnly(attributes.getBoolean("readOnly")); rbta.setQualifier(attributes.getString("value")); List<RollbackRuleAttribute> rollbackRules = new ArrayList<>(); for (Class<?> rbRule : attributes.getClassArray("rollbackFor")) { rollbackRules.add(new RollbackRuleAttribute(rbRule)); } for (String rbRule : attributes.getStringArray("rollbackForClassName")) { rollbackRules.add(new RollbackRuleAttribute(rbRule)); } for (Class<?> rbRule : attributes.getClassArray("noRollbackFor")) { rollbackRules.add(new NoRollbackRuleAttribute(rbRule)); } for (String rbRule : attributes.getStringArray("noRollbackForClassName")) { rollbackRules.add(new NoRollbackRuleAttribute(rbRule)); } rbta.setRollbackRules(rollbackRules); return rbta; }
② final TransactionManager tm = determineTransactionManager(txAttr):获取事务管理器transactionManager
- 从@Transactional注解中value属性获取,
- value未设置,则<tx:annotationDriven/>中transaction-manager属性获取,默认transaction-manager="transactionManager"
/* org.springframework.transaction.interceptor.TransactionAspectSupport#determineTransactionManager */ protected TransactionManager determineTransactionManager(@Nullable TransactionAttribute txAttr) { // Do not attempt to lookup tx manager if no tx attributes are set if (txAttr == null || this.beanFactory == null) { //事务属性为空 或 IOC容器为空时 返回null return getTransactionManager(); } String qualifier = txAttr.getQualifier(); if (StringUtils.hasText(qualifier)) { //返回@Transactional中指定的transactionManager // @Transactional(value="transactionManager")中的value属性,beanName = value return determineQualifiedTransactionManager(this.beanFactory, qualifier); } else if (StringUtils.hasText(this.transactionManagerBeanName)) { //@Transactional注解value为空时 //返回<tx:annotation-driven transaction-manager=""/>中的transaction-manager属性, // beanName = transaction-manager设置的属性,默认为"transactionManager" return determineQualifiedTransactionManager(this.beanFactory, this.transactionManagerBeanName); } else { //这个分支基本不会走到,上面transactionManagerBeanName有默认值 TransactionManager defaultTransactionManager = getTransactionManager(); if (defaultTransactionManager == null) { defaultTransactionManager = this.transactionManagerCache.get(DEFAULT_TRANSACTION_MANAGER_KEY); if (defaultTransactionManager == null) { defaultTransactionManager = this.beanFactory.getBean(TransactionManager.class); this.transactionManagerCache.putIfAbsent( DEFAULT_TRANSACTION_MANAGER_KEY, defaultTransactionManager); } } return defaultTransactionManager; } }
③ transactionManager的两个分类
- 反应式事务管理器,顶级接口ReactiveTransactionManager,例如MongoDB中事务管理器
- 响应式事务管理器,顶级接口PlatformTransactionManager,例如常用的关系型数据库MySQL等
if (this.reactiveAdapterRegistry != null && tm instanceof ReactiveTransactionManager) { //反应式事务管理器,是spring5.2之后加入的新支持,反正会用到这个东西的公司我也进不了,直接跳过吧 ReactiveTransactionSupport txSupport = this.transactionSupportCache.computeIfAbsent(method, key -> { if (KotlinDetector.isKotlinType(method.getDeclaringClass()) && KotlinDelegate.isSuspend(method)) { 杨老师课堂之springAOP事务控制源码解析