广告
返回顶部
首页 > 资讯 > 后端开发 > Python >spring是如何实现声明式事务的
  • 862
分享到

spring是如何实现声明式事务的

2024-04-02 19:04:59 862人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

前言 今天我们来讲讲spring的声明式事务。 开始 说到声明式事务,我们现在回顾一下事务这个概念,什么是事务呢,事务指的是逻辑上的⼀组操作,组成这组操作的各个单

前言

今天我们来讲讲spring的声明式事务

开始

说到声明式事务,我们现在回顾一下事务这个概念,什么是事务呢,事务指的是逻辑上的⼀组操作,组成这组操作的各个单元,要么全部成功,要么全部不成功。从而确保了数据的准确与安全。事务有着四大特性(ACID),分别是
原子性(Atomicity)原⼦性是指事务是⼀个不可分割的⼯作单位,事务中的操作要么都发⽣,要么都不发⽣。

⼀致性(Consistency)事务必须使数据库从⼀个⼀致性状态变换到另外⼀个⼀致性状态。

隔离性(Isolation)事务的隔离性是多个⽤户并发访问数据库时,数据库为每⼀个⽤户开启的事务,每个事务不能被其他事务的操作数据所⼲扰,多个并发事务之间要相互隔离。

持久性(Durability) 持久性是指⼀个事务⼀旦被提交,它对数据库中数据的改变就是永久性的,接下来即使数据库发⽣故障

也不应该对其有任何影响。

在spring中,一共有两种方式可以实现事务控制,分别是编程式事务和声明式事务。编程式事务指的是在代码中添加事务控制代码,而声明式事务指的是利用xml或者注解的形式来配置控制事务,下面就以纯注解配置声明式事务为例进行剖析。

spring开启声明式事务的注解是@EnableTransactionManagement,讲到这里首先要明白一点,spring的事务管理器管理事务其实就是利用aop的方式,通过创建动态代理加上拦截,实现的事务管理。在spring的配置类中加上这个注解,就支持了声明式事务,那么spring是怎么通过这么一个注解就可以支持事务的呢,我们来看代码。
首先我们看到,在这个注解上,import了一个selector


@Import(TransactionManagementConfigurationSelector.class)

我们看这个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这两个类,我们先来看AutoProxyRegistrar这个类,这个类中有一段这样的代码


if (mode == AdviceMode.PROXY) {
    //重要的是这句代码
 AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry);
 if ((Boolean) proxyTargetClass) {
  AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
  return;
 }
}

//我们进到这个方法中
@Nullable
 public static BeanDefinition registerAutoProxyCreatorIfNecessary(
   BeanDefinitionRegistry registry, @Nullable Object source) {
        //可以看到引入了InfrastructureAdvisorAutoProxyCreator这个类,那么这个类又是什么呢
  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);
 }
}

看一下继承结构图

可以看到这个方法间接继承于SmartInstantiationAwareBeanPostProcessor,最终继承于BeanPostProcessor,这说明InfrastructureAdvisorAutoProxyCreator类是一个后置处理器,并且跟 spring AOP 开启@EnableAspectJAutoProxy 时注册的AnnotationAwareAspectJProxyCreator实现的是同⼀个接口,这也对应了我之前所说声明式事务是springAOP思想的一种应用。

然后我们回过头来再看ProxyTransactionManagementConfiguration这个类,我们看到其中有一个事务增强器,一个属性解析器和是一个事务拦截器


    @Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
 @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
 public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor(
   TransactionAttributeSource transactionAttributeSource, TransactionInterceptor transactionInterceptor) {

  // 事务增强器
  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(TransactionAttributeSource transactionAttributeSource) {
  TransactionInterceptor interceptor = new TransactionInterceptor();
  interceptor.setTransactionAttributeSource(transactionAttributeSource);
  if (this.txManager != null) {
   interceptor.setTransactionManager(this.txManager);
  }
  return interceptor;
 }

先看看属性解析器


//注解解析器集合
 private final Set<TransactionAnnotationParser> annotationParsers;

这是一个注解解析器的集合,可以添加多种注解解析器,在这里我们主要关注的是spring事务注解解析器SpringTransactionParser,看一下相关代码


protected TransactionAttribute parseTransactionAnnotation(AnnotationAttributes attributes) {
  RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();

  // 对应Transaction注解的相关属性
  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其实正是@Transaction注解中的相关属性,这个属性解析器的作用之一就是用来解析@Transaction注解中的属性
看完了属性解析器,我们接下来看事务拦截器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...
  // 增加事务支持
  return invokeWithinTransaction(invocation.getMethod(), targetClass, invocation::proceed);
 }

然后我们进到这个方法里面


@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配置时加入
  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);
  }
  // 获取事务管理器
  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
    // 目标方法抛异常,会执行回滚的操作
    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);
    }
   }
   // 目标方法正常运行,会执行commitTransactionAfterReturning,执行事务提交操作
   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;
   }
  }
 }

总结

总体来说,spring实现声明式事务的过程是这样的

  • @EnableTransactionManagement 注解,通过@import引⼊了TransactionManagementConfigurationSelector类,它的selectImports⽅法导⼊了另外两个类:AutoProxyRegistrar和ProxyTransactionManagementConfiguration
  • AutoProxyRegistrar类中方法registerBeanDefinitions中,通过 AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry)引⼊InfrastructureAdvisorAutoProxyCreator,是一个后置处理器类
  • ProxyTransactionManagementConfiguration 是⼀个添加了@Configuration注解的配置类,注册了事务增强器(注⼊属性解析器、事务拦截器)AnnotationTransactionAttributeSource和TransactionInterceptor,AnnotationTransactionAttributeSource内部持有了⼀个解析器集合 Set annotationParsers,具体使⽤的是SpringTransactionAnnotationParser解析器,用来解析@Transactional的事务属性,事务拦截器TransactionInterceptor实现了MethodInterceptor接⼝,该通用拦截会在产⽣代理对象之前和aop增强合并,最终⼀起影响到代理对象,TransactionInterceptor的invoke⽅法中invokeWithinTransaction会触发原有业务逻辑调用(增强事务)

到此这篇关于spring是如何实现声明式事务的的文章就介绍到这了,更多相关spring 声明式事务内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: spring是如何实现声明式事务的

本文链接: https://www.lsjlt.com/news/124004.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • spring是如何实现声明式事务的
    前言 今天我们来讲讲spring的声明式事务。 开始 说到声明式事务,我们现在回顾一下事务这个概念,什么是事务呢,事务指的是逻辑上的⼀组操作,组成这组操作的各个单...
    99+
    2022-11-12
  • spring注解如何开启声明式事务
    目录spring开启声明式事务导入依赖配置类文件业务逻辑编写测试代码总结spring开启声明式事务 导入依赖 pom.xml <dependencies>        ...
    99+
    2022-12-28
    spring 注解 注解开启声明式事务 spring声明式事务
  • spring事务及声明式事务的使用方法
    这篇文章主要介绍spring事务及声明式事务的使用方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!本篇文章给大家带来的内容是关于如何理解spring事务及声明式事务的使用,包括数据...
    99+
    2022-10-18
  • Spring中编程式事务与声明式事务的区别是什么
    本篇文章为大家展示了Spring中编程式事务与声明式事务的区别是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。Spring事务属性我们都知道事务有开始,保存点,提交,回滚,隔离级别等属性。那么S...
    99+
    2023-05-31
    spring 别是
  • 如何在Spring中使用声明式事务管理
    本篇文章为大家展示了如何在Spring中使用声明式事务管理,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。基于TransactionProxyFactoryBean的事务管理配置Spring中配置AO...
    99+
    2023-05-31
    spring 声明式事务管理
  • Spring声明式事务注解的源码分析
    本文小编为大家详细介绍“Spring声明式事务注解的源码分析”,内容详细,步骤清晰,细节处理妥当,希望这篇“Spring声明式事务注解的源码分析”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。1、@EnableTr...
    99+
    2023-07-02
  • 使用springboot如何实现开启声明式事务
    这期内容当中小编将会给大家带来有关使用springboot如何实现开启声明式事务,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。springboot开启事务很简单,只需要一个注解@Transactional...
    99+
    2023-05-31
    springboot 声明式事务
  • 关于Spring中声明式事务的使用详解
    目录一、前言 二、回顾JDBC的数据库事务 三、数据库事务隔离级别 3.1 数据库事务的基本特征3.2 详解数据库隔离级别 3.2.1 未提交读 3.2.2 读提交 3.2.3 可重...
    99+
    2022-11-12
  • Spring深入刨析声明式事务注解的源码
    目录1、@EnableTransactionManagement2、加载事务控制组件2.1、AutoProxyRegistrar2.2、ProxyTransactionManagem...
    99+
    2022-11-13
  • spring boot基于注解的声明式事务配置详解
    事务配置 1、配置方式一 1)开启spring事务管理,在spring boot启动类添加注解@EnableTransactionManagement(proxyTargetClas...
    99+
    2022-11-12
  • 完美解决Spring声明式事务不回滚的问题
    疑问,确实像往常一样在service上添加了注解 @Transactional,为什么查询数据库时还是发现有数据不一致的情况,想想肯定是事务没起作用,出现异常的时候数据没有回滚。于是就对相关代码进行了一番测试,结果发现一下踩进了两个坑,确实...
    99+
    2023-05-31
    spring 声明式事务 不回滚
  • 如何解决spring声明式事务@Transactional不回滚的多种情况问题
    这篇文章给大家分享的是有关如何解决spring声明式事务@Transactional不回滚的多种情况问题的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。一、 spring 事务原理一、Spring事务原理在使用JD...
    99+
    2023-06-25
  • spring boot基于注解声明式事务配置的示例分析
    小编给大家分享一下spring boot基于注解声明式事务配置的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!事务配置1、配置方式一1)开启spring事务管理,在spring boot启动类添加注解@Enable...
    99+
    2023-06-20
  • 如何进行Spring声明性事务常见问题分析
    小编今天带大家了解如何进行Spring声明性事务常见问题分析,文中知识点介绍的非常详细。觉得有帮助的朋友可以跟着小编一起浏览文章的内容,希望能够帮助更多想解决这个问题的朋友找到问题的答案,下面跟着小编一起深入学习“如何进行Spring声明性...
    99+
    2023-06-17
  • spring声明式事务 @Transactional 不回滚的多种情况以及解决方案
    目录一、 spring 事务原理问题一、@Transactional 应该加到什么地方,如果加到Controller会回滚吗?问题二、 @Transactional 注解中用不用加r...
    99+
    2022-11-12
  • 解读Spring事务是如何实现的
    目录Spring事务如何实现Spring事务实现的几种方式编程式事务管理声明式事务管理总结Spring事务如何实现 1.Spring事务底层是基于数据库事务和AOP机制的 2.首先对...
    99+
    2023-03-19
    Spring事务 Spring事务实现 Spring如何实现事务
  • Spring Boot + RabbitMQ如何实现分布式事务
    小编给大家分享一下Spring Boot + RabbitMQ如何实现分布式事务,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!一:分布式事务解决方案1.两阶段提交(2PC)第一阶段:事务协调器要求每个涉及到事务的数据库预提...
    99+
    2023-06-03
  • Spring事务是怎么实现的
    本文小编为大家详细介绍“Spring事务是怎么实现的”,内容详细,步骤清晰,细节处理妥当,希望这篇“Spring事务是怎么实现的”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。Spring事务如何实现Spring事...
    99+
    2023-07-05
  • spring事务实现的方式有哪些
    Spring事务的实现方式有以下几种:1. 基于注解的方式:使用@Transactional注解来标识需要被事务管理的方法或类。可以...
    99+
    2023-09-28
    spring
  • Golang中map的声明定义如何实现
    本篇内容主要讲解“Golang中map的声明定义如何实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Golang中map的声明定义如何实现”吧!定义map,在golang中定义为map[str...
    99+
    2023-07-05
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作