国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

Spring事務處理原理步驟詳解

瀏覽:4日期: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
相關文章:
主站蜘蛛池模板: 精品国产一区二区三区不卡在线 | 天堂视频网站 | 欧美日韩亚洲综合在线一区二区 | 亚洲视频在线观看 | 亚洲国产精品线播放 | 国产精品日韩欧美一区二区 | 在线免费视频国产 | 国产欧美日韩精品一区二区三区 | 亚洲精品在线免费观看视频 | 99re热精品这里精品 | 国产成人精品aaaa视频一区 | 性夜影院爽黄a爽免费看网站 | 69xx欧美| 久久手机精品视频 | 欧美第一视频 | 一本色道久久88亚洲精品综合 | 欧美日韩一区二区在线观看视频 | 在线不卡一区 | 99免费在线视频 | 国产婷婷一区二区在线观看 | 精品一区二区在线欧美日韩 | 日本免费视 | 亚洲国产激情一区二区三区 | 午夜精品视频在线观看美女 | 欧美日韩精品一区二区三区 | 一级a美女毛片 | 在线亚洲综合 | 亚洲综合国产一区二区三区 | 99je全部都是精品视频在线 | 成年女人免费看片 | 日韩免费一区二区三区 | 日韩亚洲一区中文字幕 | m男亚洲一区中文字幕 | 国产精品久久久天天影视香蕉 | www国产视频 | 国产精品三级在线观看 | 国产九九在线观看播放 | 国产小视频在线高清播放 | 小屁孩cao大人免费网站 | 国产看午夜精品理论片 | 亚洲高清免费视频 |