spring是如何實(shí)現(xiàn)聲明式事務(wù)的
前言
今天我們來(lái)講講spring的聲明式事務(wù)。
開始
說(shuō)到聲明式事務(wù),我們現(xiàn)在回顧一下事務(wù)這個(gè)概念,什么是事務(wù)呢,事務(wù)指的是邏輯上的⼀組操作,組成這組操作的各個(gè)單元,要么全部成功,要么全部不成功。從而確保了數(shù)據(jù)的準(zhǔn)確與安全。事務(wù)有著四大特性(ACID),分別是
原子性(Atomicity)原⼦性是指事務(wù)是⼀個(gè)不可分割的⼯作單位,事務(wù)中的操作要么都發(fā)⽣,要么都不發(fā)⽣。
⼀致性(Consistency)事務(wù)必須使數(shù)據(jù)庫(kù)從⼀個(gè)⼀致性狀態(tài)變換到另外⼀個(gè)⼀致性狀態(tài)。
隔離性(Isolation)事務(wù)的隔離性是多個(gè)⽤戶并發(fā)訪問(wèn)數(shù)據(jù)庫(kù)時(shí),數(shù)據(jù)庫(kù)為每⼀個(gè)⽤戶開啟的事務(wù),每個(gè)事務(wù)不能被其他事務(wù)的操作數(shù)據(jù)所⼲擾,多個(gè)并發(fā)事務(wù)之間要相互隔離。
持久性(Durability) 持久性是指⼀個(gè)事務(wù)⼀旦被提交,它對(duì)數(shù)據(jù)庫(kù)中數(shù)據(jù)的改變就是永久性的,接下來(lái)即使數(shù)據(jù)庫(kù)發(fā)⽣故障
也不應(yīng)該對(duì)其有任何影響。
在spring中,一共有兩種方式可以實(shí)現(xiàn)事務(wù)控制,分別是編程式事務(wù)和聲明式事務(wù)。編程式事務(wù)指的是在代碼中添加事務(wù)控制代碼,而聲明式事務(wù)指的是利用xml或者注解的形式來(lái)配置控制事務(wù),下面就以純注解配置聲明式事務(wù)為例進(jìn)行剖析。
spring開啟聲明式事務(wù)的注解是@EnableTransactionManagement,講到這里首先要明白一點(diǎn),spring的事務(wù)管理器管理事務(wù)其實(shí)就是利用aop的方式,通過(guò)創(chuàng)建動(dòng)態(tài)代理加上攔截,實(shí)現(xiàn)的事務(wù)管理。在spring的配置類中加上這個(gè)注解,就支持了聲明式事務(wù),那么spring是怎么通過(guò)這么一個(gè)注解就可以支持事務(wù)的呢,我們來(lái)看代碼。
首先我們看到,在這個(gè)注解上,import了一個(gè)selector
@Import(TransactionManagementConfigurationSelector.class)
我們看這個(gè)selector類中的這么一段代碼
@Override protected String[] selectImports(AdviceMode adviceMode) { switch (adviceMode) { case PROXY: return new String[] {AutoProxyRegistrar.class.getName(), ProxyTransactionManagementConfiguration.class.getName()}; case ASPECTJ: return new String[] {determineTransactionAspectClass()}; default: return null; } }
這段代碼中,引入了AutoProxyRegistrar和ProxyTransactionManagementConfiguration這兩個(gè)類,我們先來(lái)看AutoProxyRegistrar這個(gè)類,這個(gè)類中有一段這樣的代碼
if (mode == AdviceMode.PROXY) { //重要的是這句代碼 AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry); if ((Boolean) proxyTargetClass) { AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry); return; } } //我們進(jìn)到這個(gè)方法中 @Nullable public static BeanDefinition registerAutoProxyCreatorIfNecessary( BeanDefinitionRegistry registry, @Nullable Object source) { //可以看到引入了InfrastructureAdvisorAutoProxyCreator這個(gè)類,那么這個(gè)類又是什么呢 return registerOrEscalateApcAsRequired(InfrastructureAdvisorAutoProxyCreator.class, registry, source); } //先看一下 public class InfrastructureAdvisorAutoProxyCreator extends AbstractAdvisorAutoProxyCreator { @Nullable private ConfigurableListableBeanFactory beanFactory; @Override protected void initBeanFactory(ConfigurableListableBeanFactory beanFactory) { super.initBeanFactory(beanFactory); this.beanFactory = beanFactory; } @Override protected boolean isEligibleAdvisorBean(String beanName) { return (this.beanFactory != null && this.beanFactory.containsBeanDefinition(beanName) && this.beanFactory.getBeanDefinition(beanName).getRole() == BeanDefinition.ROLE_INFRASTRUCTURE); } }
看一下繼承結(jié)構(gòu)圖
可以看到這個(gè)方法間接繼承于SmartInstantiationAwareBeanPostProcessor,最終繼承于BeanPostProcessor,這說(shuō)明InfrastructureAdvisorAutoProxyCreator類是一個(gè)后置處理器,并且跟 spring AOP 開啟@EnableAspectJAutoProxy 時(shí)注冊(cè)的AnnotationAwareAspectJProxyCreator實(shí)現(xiàn)的是同⼀個(gè)接口,這也對(duì)應(yīng)了我之前所說(shuō)聲明式事務(wù)是springAOP思想的一種應(yīng)用。
然后我們回過(guò)頭來(lái)再看ProxyTransactionManagementConfiguration這個(gè)類,我們看到其中有一個(gè)事務(wù)增強(qiáng)器,一個(gè)屬性解析器和是一個(gè)事務(wù)攔截器
@Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME) @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor( TransactionAttributeSource transactionAttributeSource, TransactionInterceptor transactionInterceptor) { // 事務(wù)增強(qiáng)器 BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor(); // 注入屬性解析器 advisor.setTransactionAttributeSource(transactionAttributeSource); // 注入事務(wù)攔截器 advisor.setAdvice(transactionInterceptor); if (this.enableTx != null) { advisor.setOrder(this.enableTx.<Integer>getNumber("order")); } return advisor; } @Bean @Role(BeanDefinition.ROLE_INFRASTRUCTURE) // 屬性解析器 public TransactionAttributeSource transactionAttributeSource() { return new AnnotationTransactionAttributeSource(); } @Bean @Role(BeanDefinition.ROLE_INFRASTRUCTURE) // 事務(wù)攔截器 public TransactionInterceptor transactionInterceptor(TransactionAttributeSource transactionAttributeSource) { TransactionInterceptor interceptor = new TransactionInterceptor(); interceptor.setTransactionAttributeSource(transactionAttributeSource); if (this.txManager != null) { interceptor.setTransactionManager(this.txManager); } return interceptor; }
先看看屬性解析器
//注解解析器集合 private final Set<TransactionAnnotationParser> annotationParsers;
這是一個(gè)注解解析器的集合,可以添加多種注解解析器,在這里我們主要關(guān)注的是spring事務(wù)注解解析器SpringTransactionParser,看一下相關(guān)代碼
protected TransactionAttribute parseTransactionAnnotation(AnnotationAttributes attributes) { RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute(); // 對(duì)應(yīng)Transaction注解的相關(guān)屬性 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; }
可以看到這段代碼中的Enum和ClassArray其實(shí)正是@Transaction注解中的相關(guān)屬性,這個(gè)屬性解析器的作用之一就是用來(lái)解析@Transaction注解中的屬性
看完了屬性解析器,我們接下來(lái)看事務(wù)攔截器TransactionInterceptor,其中重要的是這段代碼
@Override @Nullable public Object invoke(MethodInvocation invocation) throws Throwable { // Work out the target class: may be {@code null}. // The TransactionAttributeSource should be passed the target class // as well as the method, which may be from an interface. Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null); // Adapt to TransactionAspectSupport's invokeWithinTransaction... // 增加事務(wù)支持 return invokeWithinTransaction(invocation.getMethod(), targetClass, invocation::proceed); }
然后我們進(jìn)到這個(gè)方法里面
@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. // 獲取屬性解析器,在配置類ProxyTransactionManagementConfiguration配置時(shí)加入 TransactionAttributeSource tas = getTransactionAttributeSource(); final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null); final TransactionManager tm = determineTransactionManager(txAttr); if (this.reactiveAdapterRegistry != null && tm instanceof ReactiveTransactionManager) { 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); } // 獲取事務(wù)管理器 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 // 目標(biāo)方法拋異常,會(huì)執(zhí)行回滾的操作 completeTransactionAfterThrowing(txInfo, ex); throw ex; } finally { 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); } } // 目標(biāo)方法正常運(yùn)行,會(huì)執(zhí)行commitTransactionAfterReturning,執(zhí)行事務(wù)提交操作 commitTransactionAfterReturning(txInfo); return retVal; } else { 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; } } }
總結(jié)
總體來(lái)說(shuō),spring實(shí)現(xiàn)聲明式事務(wù)的過(guò)程是這樣的
- @EnableTransactionManagement 注解,通過(guò)@import引⼊了TransactionManagementConfigurationSelector類,它的selectImports⽅法導(dǎo)⼊了另外兩個(gè)類:AutoProxyRegistrar和ProxyTransactionManagementConfiguration
- AutoProxyRegistrar類中方法registerBeanDefinitions中,通過(guò) AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry)引⼊InfrastructureAdvisorAutoProxyCreator,是一個(gè)后置處理器類
- ProxyTransactionManagementConfiguration 是⼀個(gè)添加了@Configuration注解的配置類,注冊(cè)了事務(wù)增強(qiáng)器(注⼊屬性解析器、事務(wù)攔截器)AnnotationTransactionAttributeSource和TransactionInterceptor,AnnotationTransactionAttributeSource內(nèi)部持有了⼀個(gè)解析器集合 Set annotationParsers,具體使⽤的是SpringTransactionAnnotationParser解析器,用來(lái)解析@Transactional的事務(wù)屬性,事務(wù)攔截器TransactionInterceptor實(shí)現(xiàn)了MethodInterceptor接⼝,該通用攔截會(huì)在產(chǎn)⽣代理對(duì)象之前和aop增強(qiáng)合并,最終⼀起影響到代理對(duì)象,TransactionInterceptor的invoke⽅法中invokeWithinTransaction會(huì)觸發(fā)原有業(yè)務(wù)邏輯調(diào)用(增強(qiáng)事務(wù))
到此這篇關(guān)于spring是如何實(shí)現(xiàn)聲明式事務(wù)的的文章就介紹到這了,更多相關(guān)spring 聲明式事務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中的類型自動(dòng)轉(zhuǎn)換機(jī)制解析
這篇文章主要介紹了java中的類型自動(dòng)轉(zhuǎn)換機(jī)制,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03java實(shí)現(xiàn)聯(lián)機(jī)五子棋
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)聯(lián)機(jī)五子棋,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Java實(shí)現(xiàn)的簡(jiǎn)單音樂(lè)播放器功能示例
這篇文章主要介紹了Java實(shí)現(xiàn)的簡(jiǎn)單音樂(lè)播放器功能,涉及java針對(duì)多媒體文件相關(guān)載入、播放相關(guān)操作技巧,需要的朋友可以參考下2019-02-02MyBatis傳入集合 list 數(shù)組 map參數(shù)的寫法
這篇文章主要介紹了MyBatis傳入集合 list 數(shù)組 map參數(shù)的寫法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06Java如何獲取對(duì)象屬性及對(duì)應(yīng)值
這篇文章主要介紹了Java如何獲取對(duì)象屬性及對(duì)應(yīng)值,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11