av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

spring是如何實(shí)現(xiàn)聲明式事務(wù)的

瀏覽:37日期:2023-07-17 14:21:37
前言

今天我們來(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)圖

spring是如何實(shí)現(xiàn)聲明式事務(wù)的

可以看到這個(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)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产精品伦一区二区三级视频 | 国产精品一区久久久 | 特级黄一级播放 | 希岛爱理在线 | 精品欧美一区二区三区精品久久 | 你懂的在线视频播放 | 国产精品视频播放 | 欧美日韩精品一区二区天天拍 | 欧美极品在线观看 | 国内精品久久久久久久影视简单 | 国产aa| 国产乱码精品一区二区三区中文 | 国产免费一区二区三区 | 天堂一区二区三区四区 | 亚洲国产乱码 | 日韩欧美中文字幕在线观看 | 大学生a级毛片免费视频 | 国产精品网页 | 国产欧美一区二区三区在线看 | 精品国产欧美一区二区三区不卡 | 国产精品第2页 | 欧美在线激情 | 男女羞羞视频免费看 | 成人精品国产一区二区4080 | 日韩成人免费视频 | 免费麻豆视频 | 午夜精品久久久久99蜜 | 宅男噜噜噜66一区二区 | 99国产视频| 成人国产精品久久久 | 久久夜视频 | 日韩在线观看网站 | 在线播放国产一区二区三区 | 99pao成人国产永久免费视频 | 国产一区二区三区四区 | 操久久久 | 九色av| 国产精品久久久久久久久久 | 成人久久久 | 夜夜撸av| 中文字幕日韩一区 |