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

您的位置:首頁技術文章
文章詳情頁

Spring事務處理原理步驟詳解

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

1、事務處理實現

實現步驟:

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

代碼實現:

@EnableTransactionManagement@ComponentScan('com.atguigu.tx')@Configurationpublic class TxConfig { //數據源 @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對@Configuration類會特殊處理;給容器中加組件的方法,多次調用都只是從容器中找組件 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource()); return jdbcTemplate; } //注冊事務管理器在容器中 @Bean public PlatformTransactionManager transactionManager() throws Exception{ return new DataSourceTransactionManager(dataSource()); }} 

2、事務處理原理

原理分析:

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

核心代碼

1、EnableTransactionManagement注解,注入TransactionManagementConfigurationSelector類

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

2、TransactionManagementConfigurationSelector類,最終會導入AutoProxyRegistrar.class和ProxyTransactionManagementConfiguration.class兩個組件。

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類的作用為:

給容器中注冊一個 InfrastructureAdvisorAutoProxyCreator 組件;

最終的目的是:利用后置處理器機制在對象創建以后,包裝對象,返回一個代理對象(增強器),代理對象執行方法利用攔截器鏈進行調用;

@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事務處理原理步驟詳解

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

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

4、ProxyTransactionManagementConfiguration類

代理事務管理配置類

@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類,事務調用:invokeWithinTransaction()方法為最終執行的方法

@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類的最終事務方法執行:

@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; } } }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
日韩精品国产欧美| 免费日韩一区二区三区| 日韩欧美一区二区三区免费看| 国产精品久久久久久久久久齐齐 | 国产一区二区三区自拍| 精品捆绑调教一区二区三区| 999久久久精品国产| 婷婷亚洲五月色综合| 蜜臀精品久久久久久蜜臀 | 日韩大片免费观看| 国产一区日韩欧美| 亚洲日本三级| 国产精品v一区二区三区| 国产91在线播放精品| 亚洲精品88| 久久久人人人| 欧美专区在线| 国产激情欧美| 久久精品国产www456c0m| 蜜臀av亚洲一区中文字幕| 国产精品一页| 久久精品成人| 日本99精品| 日韩精品中文字幕第1页| 国产一区白浆| 免费亚洲婷婷| 日韩欧美一区二区三区免费看| 免费久久99精品国产自在现线| 国产精品久久久亚洲一区| 国产中文在线播放| 免费视频一区二区| 国产69精品久久| 亚洲中字黄色| 另类小说一区二区三区| re久久精品视频| 欧美日韩精品一区二区三区视频 | 亚洲人成网站在线在线观看| 精品视频一区二区三区在线观看 | 亚洲一区二区成人| 美女精品一区二区| 韩日一区二区三区| 国产经典一区| 免费看日韩精品| 国产传媒av在线| 亚洲精一区二区三区| 国产精品久久久久蜜臀| 日韩一区二区三区高清在线观看| 国产中文在线播放| 日本va欧美va精品| 国产一区久久| 高清一区二区| 日韩av在线播放中文字幕| 91精品精品| 国产精品xxx| 在线看片日韩| 今天的高清视频免费播放成人| 麻豆精品在线视频| 天堂va欧美ⅴa亚洲va一国产| 香蕉精品久久| 国产成人精品一区二区三区视频| 色8久久久久| 欧美午夜精品一区二区三区电影| 国产精品巨作av| 日韩手机在线| 国产精品视区| 日韩一区二区在线免费| 国产精品久久久久9999高清| 石原莉奈一区二区三区在线观看| 日韩欧美一区二区三区免费看| 国产精品多人| 欧美有码在线| 日韩影院免费视频| 一区视频在线| 欧美成人国产| 久久久噜噜噜| 久久国产毛片| 国产在线看片免费视频在线观看| 欧美激情视频一区二区三区免费 | 日本一区二区高清不卡| 日本欧美大码aⅴ在线播放| 国产精品毛片在线| 欧美日韩四区| 一区免费在线| 亚洲综合不卡| 黑丝一区二区| 欧美成人精品| 欧美日韩免费观看一区=区三区| 亚洲1234区| 天堂√中文最新版在线| 97精品中文字幕| 国产suv精品一区| 国产成人调教视频在线观看| 老司机精品视频在线播放| 国产精品一区二区美女视频免费看| 老牛国产精品一区的观看方式| 伊人精品视频| 久久av在线| 免费人成网站在线观看欧美高清| 老鸭窝毛片一区二区三区| 每日更新成人在线视频| 午夜在线播放视频欧美| 天堂成人国产精品一区| 美国三级日本三级久久99| 亚洲午夜久久| 日本麻豆一区二区三区视频| 日韩国产欧美三级| 国产精品chinese| 久久精品毛片| 亚洲1234区| 欧美一区二区三区高清视频 | 亚洲天堂一区二区| 国产真实久久| 亚洲影视一区二区三区| 欧美视频精品全部免费观看| 国产精品激情| 国产日韩电影| 亚洲激情黄色| 午夜精品影视国产一区在线麻豆| 久久国产乱子精品免费女| 久久中文字幕一区二区三区| 在线一区av| 免费日韩视频| 日韩精品乱码av一区二区| 欧美激情五月| 亚洲手机在线| 日韩手机在线| xxxxx性欧美特大| 日韩中文欧美在线| 久久wwww| 国户精品久久久久久久久久久不卡 | 91精品视频一区二区| 久久99青青| 国产专区一区| 色8久久久久| 麻豆网站免费在线观看| 野花国产精品入口| 久久超级碰碰| 欧美精品激情| 国产精品毛片视频| 国产在线日韩| 国产精品一站二站| 九一精品国产| 国产精品s色| 黄色亚洲大片免费在线观看| 国产欧美欧美| 欧美a级片一区| 国产精品对白久久久久粗| 日本在线精品| 视频精品一区二区| 精品视频91| 亚洲一二三区视频| 日韩激情一区| 国产欧美69| 欧美另类综合| 精品一区二区男人吃奶 | av高清不卡| 日本欧美大码aⅴ在线播放| 久久人人88| 久久久久九九精品影院| 久久亚洲电影| 久久精品网址| 亚洲3区在线| 不卡中文一二三区| 福利一区二区三区视频在线观看| 中文无码日韩欧| 91精品国产自产在线观看永久∴| 国产三级精品三级在线观看国产| av亚洲在线观看| 岛国av在线网站| 国产精品videosex极品| 亚洲我射av| 在线国产一区二区| 国产精品久久久久久久久久10秀| 日本成人中文字幕在线视频| 中文国产一区| 91看片一区| 精品一区二区三区视频在线播放| 亚洲精品九九| 亚洲激情av| 久久久水蜜桃av免费网站| 久久免费福利| 国产日韩欧美一区二区三区在线观看| 亚洲少妇在线| 欧美91福利在线观看| 激情国产在线| 麻豆精品视频在线观看视频| 日韩av一区二区三区四区| 蜜桃视频一区二区三区| 在线视频亚洲| aa亚洲婷婷| 国产一区日韩一区| 欧美日韩在线网站| 欧美日韩视频免费观看| 欧美国产美女| 国内自拍视频一区二区三区| 久久xxx视频| 欧美国产日韩电影| 日日摸夜夜添夜夜添国产精品| 一区二区91| 影音先锋久久精品|