日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

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

Spring事務(wù)處理原理步驟詳解

瀏覽:20日期:2023-09-14 09:31:44

1、事務(wù)處理實(shí)現(xiàn)

實(shí)現(xiàn)步驟:

* 聲明式事務(wù):** 環(huán)境搭建:* 1、導(dǎo)入相關(guān)依賴* 數(shù)據(jù)源、數(shù)據(jù)庫驅(qū)動(dòng)、Spring-jdbc模塊* 2、配置數(shù)據(jù)源、JdbcTemplate(Spring提供的簡化數(shù)據(jù)庫操作的工具)操作數(shù)據(jù)* 3、給方法上標(biāo)注 @Transactional 表示當(dāng)前方法是一個(gè)事務(wù)方法;* 4、 @EnableTransactionManagement 開啟基于注解的事務(wù)管理功能;* @EnableXXX* 5、配置事務(wù)管理器來控制事務(wù);* @Bean* public PlatformTransactionManager transactionManager()

代碼實(shí)現(xiàn):

@EnableTransactionManagement@ComponentScan('com.atguigu.tx')@Configurationpublic class TxConfig { //數(shù)據(jù)源 @Bean public DataSource dataSource() throws Exception{ ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setUser('root'); dataSource.setPassword('123456'); dataSource.setDriverClass('com.mysql.jdbc.Driver'); dataSource.setJdbcUrl('jdbc:mysql://localhost:3306/test'); return dataSource; } @Bean public JdbcTemplate jdbcTemplate() throws Exception{ //Spring對(duì)@Configuration類會(huì)特殊處理;給容器中加組件的方法,多次調(diào)用都只是從容器中找組件 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource()); return jdbcTemplate; } //注冊(cè)事務(wù)管理器在容器中 @Bean public PlatformTransactionManager transactionManager() throws Exception{ return new DataSourceTransactionManager(dataSource()); }} 

2、事務(wù)處理原理

原理分析:

* 原理:* 1)、@EnableTransactionManagement* 利用TransactionManagementConfigurationSelector給容器中會(huì)導(dǎo)入組件* 導(dǎo)入兩個(gè)組件* AutoProxyRegistrar* ProxyTransactionManagementConfiguration* 2)、AutoProxyRegistrar:* 給容器中注冊(cè)一個(gè) InfrastructureAdvisorAutoProxyCreator 組件;* InfrastructureAdvisorAutoProxyCreator:?* 利用后置處理器機(jī)制在對(duì)象創(chuàng)建以后,包裝對(duì)象,返回一個(gè)代理對(duì)象(增強(qiáng)器),代理對(duì)象執(zhí)行方法利用攔截器鏈進(jìn)行調(diào)用;** 3)、ProxyTransactionManagementConfiguration 做了什么?* 1、給容器中注冊(cè)事務(wù)增強(qiáng)器;* 1)、事務(wù)增強(qiáng)器要用事務(wù)注解的信息,AnnotationTransactionAttributeSource解析事務(wù)注解* 2)、事務(wù)攔截器:* TransactionInterceptor;保存了事務(wù)屬性信息,事務(wù)管理器;* 他是一個(gè) MethodInterceptor;* 在目標(biāo)方法執(zhí)行的時(shí)候;* 執(zhí)行攔截器鏈;* 事務(wù)攔截器:* 1)、先獲取事務(wù)相關(guān)的屬性* 2)、再獲取PlatformTransactionManager,如果事先沒有添加指定任何transactionmanger* 最終會(huì)從容器中按照類型獲取一個(gè)PlatformTransactionManager;* 3)、執(zhí)行目標(biāo)方法* 如果異常,獲取到事務(wù)管理器,利用事務(wù)管理回滾操作;* 如果正常,利用事務(wù)管理器,提交事務(wù)* */

核心代碼

1、EnableTransactionManagement注解,注入TransactionManagementConfigurationSelector類

@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Import(TransactionManagementConfigurationSelector.class)public @interface EnableTransactionManagement {

2、TransactionManagementConfigurationSelector類,最終會(huì)導(dǎo)入AutoProxyRegistrar.class和ProxyTransactionManagementConfiguration.class兩個(gè)組件。

public class TransactionManagementConfigurationSelector extends AdviceModeImportSelector<EnableTransactionManagement> { /** * Returns {@link ProxyTransactionManagementConfiguration} or * {@code AspectJ(Jta)TransactionManagementConfiguration} for {@code PROXY} * and {@code ASPECTJ} values of {@link EnableTransactionManagement#mode()}, * respectively. */ @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; } } private String determineTransactionAspectClass() { return (ClassUtils.isPresent('javax.transaction.Transactional', getClass().getClassLoader()) ?TransactionManagementConfigUtils.JTA_TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME :TransactionManagementConfigUtils.TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME); } }

3、AutoProxyRegistrar類的作用為:

給容器中注冊(cè)一個(gè) InfrastructureAdvisorAutoProxyCreator 組件;

最終的目的是:利用后置處理器機(jī)制在對(duì)象創(chuàng)建以后,包裝對(duì)象,返回一個(gè)代理對(duì)象(增強(qiáng)器),代理對(duì)象執(zhí)行方法利用攔截器鏈進(jìn)行調(diào)用;

@Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { boolean candidateFound = false; Set<String> annTypes = importingClassMetadata.getAnnotationTypes(); for (String annType : annTypes) { AnnotationAttributes candidate = AnnotationConfigUtils.attributesFor(importingClassMetadata, annType); if (candidate == null) {continue; } Object mode = candidate.get('mode'); Object proxyTargetClass = candidate.get('proxyTargetClass'); if (mode != null && proxyTargetClass != null && AdviceMode.class == mode.getClass() && Boolean.class == proxyTargetClass.getClass()) {candidateFound = true;if (mode == AdviceMode.PROXY) { AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry); if ((Boolean) proxyTargetClass) { AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry); return; }} } } if (!candidateFound && logger.isInfoEnabled()) { String name = getClass().getSimpleName(); logger.info(String.format('%s was imported but no annotations were found ' + 'having both ’mode’ and ’proxyTargetClass’ attributes of type ' + 'AdviceMode and boolean respectively. This means that auto proxy ' + 'creator registration and configuration may not have occurred as ' + 'intended, and components may not be proxied as expected. Check to ' + 'ensure that %s has been @Import’ed on the same class where these ' + 'annotations are declared; otherwise remove the import of %s ' + 'altogether.', name, name, name)); } }

Spring事務(wù)處理原理步驟詳解

InfrastructureAdvisorAutoProxyCreator類的作用與AnnotationAwareAspectJAutoProxyCreator類的作用類似。

@SuppressWarnings('serial')public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware {

4、ProxyTransactionManagementConfiguration類

代理事務(wù)管理配置類

@Configurationpublic class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration { @Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME) @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor() { BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor(); advisor.setTransactionAttributeSource(transactionAttributeSource()); 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) public TransactionInterceptor transactionInterceptor() { TransactionInterceptor interceptor = new TransactionInterceptor(); interceptor.setTransactionAttributeSource(transactionAttributeSource()); if (this.txManager != null) { interceptor.setTransactionManager(this.txManager); } return interceptor; } }

TransactionInterceptor類,事務(wù)調(diào)用:invokeWithinTransaction()方法為最終執(zhí)行的方法

@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... return invokeWithinTransaction(invocation.getMethod(), targetClass, invocation::proceed); }

TransactionAspectSupport類的最終事務(wù)方法執(zhí)行:

@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. TransactionAttributeSource tas = getTransactionAttributeSource(); final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null); final PlatformTransactionManager tm = determineTransactionManager(txAttr); final String joinpointIdentification = methodIdentification(method, targetClass, txAttr); if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) { // Standard transaction demarcation with getTransaction and commit/rollback calls. TransactionInfo txInfo = createTransactionIfNecessary(tm, 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 exceptioncompleteTransactionAfterThrowing(txInfo, ex);throw ex; } finally {cleanupTransactionInfo(txInfo); } commitTransactionAfterReturning(txInfo); return retVal; } else { final ThrowableHolder throwableHolder = new ThrowableHolder(); // It’s a CallbackPreferringPlatformTransactionManager: pass a TransactionCallback in. try {Object result = ((CallbackPreferringPlatformTransactionManager) tm).execute(txAttr, status -> { TransactionInfo txInfo = prepareTransactionInfo(tm, txAttr, joinpointIdentification, status); try { return invocation.proceedWithInvocation(); } 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; } } }

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲人成亚洲精品| 欧美专区在线| 97久久超碰| 欧美中文一区| 精品日韩一区| 色爱综合网欧美| 久久美女性网| 99热精品在线| 91麻豆精品激情在线观看最新 | 欧美亚洲国产日韩| 久久99精品久久久野外观看| 麻豆一区二区三区| 视频福利一区| 日韩一区二区三免费高清在线观看| 国产无遮挡裸体免费久久| 亚洲精品高潮| 国内在线观看一区二区三区 | 日韩手机在线| 国产精品久久久久久久久久久久久久久| 久久精品天堂| 在线综合欧美| 国产精品久久久久久妇女| 久久91导航| 日本亚洲视频| 国产欧洲在线| 亚洲免费观看高清完整版在线观| 美女尤物国产一区| 国产一区欧美| 视频一区在线视频| 精品国产三区在线| 香蕉视频成人在线观看| 久久99免费视频| 99亚洲视频| 免费在线观看一区| 亚洲二区三区不卡| 精品中文在线| 中文精品电影| 麻豆mv在线观看| 性色一区二区| 日韩成人三级| 国产欧美三级| 免费观看不卡av| 欧美日韩一区二区三区四区在线观看 | 精品国产亚洲日本| 婷婷亚洲五月色综合| 久久av免费| 日韩在线成人| 欧美日韩在线网站| 国产日韩欧美三级| 欧美二区视频| 精品国产中文字幕第一页 | 精品一区二区三区中文字幕 | 色综合视频一区二区三区日韩| 国产色噜噜噜91在线精品| 国产综合视频| 欧美激情亚洲| 蜜桃91丨九色丨蝌蚪91桃色| 高清一区二区三区av| 日韩精品一级中文字幕精品视频免费观看 | 欧美+亚洲+精品+三区| 国产精品网址| 久久国产精品99国产| 国产成人77亚洲精品www| 日韩激情啪啪| 玖玖玖国产精品| 99久久婷婷| 91一区二区三区四区| 国产美女久久| 日韩福利视频网| 亚洲一区网站| 99热精品在线| 国内精品美女在线观看| 国产欧美日韩视频在线| 亚洲精品影院在线观看| 91一区二区| 国产欧美日韩亚洲一区二区三区| 美日韩精品视频| 日韩视频二区| 免费一二一二在线视频| 久久只有精品| 另类欧美日韩国产在线| 国产精品中文字幕制服诱惑| 日本亚洲三级在线| 丝袜亚洲精品中文字幕一区| 国产一区久久| 亚洲激情婷婷| 久久精品青草| 久久国产免费| 精品国产亚洲一区二区三区| 国产精品日韩精品中文字幕| 欧美亚洲二区| 久久国际精品| 日韩精品一区二区三区中文 | 国产免费成人| 视频在线观看一区| 国产精品老牛| 亚洲综合不卡| 日本中文字幕视频一区| 欧美精品三级在线| 欧美精品导航| 日本va欧美va精品发布| 青草久久视频| 精品国产精品国产偷麻豆| 麻豆视频在线观看免费网站黄| 国产aⅴ精品一区二区四区| 91视频一区| 亚洲网站视频| 老鸭窝毛片一区二区三区| 午夜亚洲福利| 久久不卡日韩美女| 蜜臀国产一区| 91久久视频| 国产乱子精品一区二区在线观看 | 欧美精品资源| 久久久影院免费| 蜜臀av国产精品久久久久| 日韩国产一区二| 福利一区视频| 日韩午夜高潮| 国产精品扒开腿做爽爽爽软件| 国产91在线播放精品| 日韩一区二区三区在线免费观看| 国产一区日韩欧美| 亚洲精品欧美| 日本aⅴ亚洲精品中文乱码 | 国产精品videossex久久发布| 国产欧美日本| 日本在线高清| 新版的欧美在线视频| 999在线观看精品免费不卡网站| 中文一区一区三区免费在线观 | 黑森林国产精品av| 久久精品观看| 一区二区三区四区精品视频| 国产精品一国产精品k频道56| 国产日韩欧美高清免费| 四虎4545www国产精品| 视频在线观看91| 国产精品色在线网站| 色在线视频观看| 亚洲激情久久| 国产精品天堂蜜av在线播放| 国产91欧美| 免费成人av在线播放| 亚洲啊v在线免费视频| 国产成人免费精品| 亚洲激情偷拍| 日韩专区欧美专区| 欧美国产亚洲精品| 波多野结衣一区| 欧美高清一区| 麻豆一区二区三| 性欧美69xoxoxoxo| 免费一级欧美片在线观看网站| 婷婷六月综合| 91精品一区| 亚洲一区亚洲| 亚洲深夜视频| 婷婷成人av| 国内激情久久| 国产 日韩 欧美 综合 一区| 亚洲精品自拍| 91欧美日韩| 91嫩草精品| 欧洲毛片在线视频免费观看| 久久婷婷国产| 日韩高清一级| 国产专区一区| 精品国产18久久久久久二百| 久久午夜视频| 欧美gv在线| 欧美激情视频一区二区三区免费 | 欧美极品中文字幕| 亚洲一二三区视频| 日韩免费看片| 久久av免费| 国产欧美69| 日本aⅴ精品一区二区三区| 亚洲免费影视| 日韩午夜精品| 尤物精品在线| 九九久久电影| 亚洲高清成人| 日本在线精品| 日本免费一区二区三区四区| 欧美激情一区| 亚洲精品美女91| 亚洲精品观看| 在线观看一区| 免费看欧美美女黄的网站| 在线亚洲观看| 一区在线免费| 在线一区欧美| 午夜日韩在线| 国产综合精品| 午夜国产一区二区| 免费看av不卡| 欧美日韩免费看片| 日韩综合一区| 麻豆国产欧美日韩综合精品二区|