美文网首页
SpringBoot 声明式事物配置

SpringBoot 声明式事物配置

作者: 赛亚人之神 | 来源:发表于2018-10-24 14:44 被阅读107次

特别注意的是定义 自定义事务拦截器 TransactionInterceptor bean 的名字不能使用 transactionInterceptor 该 bean 可能已经存在,导致该生成 bean 的方法不能执行,所以定义成另外一个名字
SpringBoot 的版本是 2.0.6.RELEASE

参考文章:
http://www.cnblogs.com/guozp/articles/7446477.html
https://gitee.com/eagleFlySky/codes/pects6ro0l4dw3qgbiz8h75

@Getter
@Setter
@Configuration
public class TransactionConfiguration {


  private static final String CUSTOM_TRANSACTION_INTERCEPTOR_NAME = "customTransactionInterceptor";
  private static final String CUSTOM_PLATFORM_TRANSACTION_MANAGER_NAME = "platformTransactionManager";
  private static final String CUSTOM_BEAN_NAME_AUTO_PROXY_CREATOR_NAME = "beanNameAutoProxyCreator";




  /**
   * 默认只对 "*Service" , "*ServiceImpl" Bean 进行事务处理,"*"表示模糊匹配, 比如 : userService,orderServiceImpl
   */
  private static final String[] DEFAULT_TRANSACTION_BEAN_NAMES = {"*Service", "*ServiceImpl"};
  /**
   * 可传播事务配置
   */
  private static final String[] DEFAULT_REQUIRED_METHOD_RULE_TRANSACTION_ATTRIBUTES = {
      "add*",
      "save*",
      "insert*",
      "delete*",
      "update*",
      "edit*",
      "batch*",
      "create*",
      "remove*",
  };

  /**
   * 默认的只读事务
   */
  private static final String[] DEFAULT_READ_ONLY_METHOD_RULE_TRANSACTION_ATTRIBUTES = {
      "get*",
      "count*",
      "find*",
      "query*",
      "select*",
      "list*",
      "*",
  };

  /**
   * 自定义事务 BeanName 拦截
   */
  private String[] customTransactionBeanNames = {};
  /**
   * 自定义方法名的事务属性相关联,可以使用通配符(*)字符关联相同的事务属性的设置方法; 只读事务
   */
  private String[] customReadOnlyMethodRuleTransactionAttributes = {};
  /**
   * 自定义方法名的事务属性相关联,可以使用通配符(*)字符关联相同的事务属性的设置方法; 传播事务(默认的)
   * {@link org.springframework.transaction.annotation.Propagation#REQUIRED}
   */
  private String[] customRequiredMethodRuleTransactionAttributes = {};

  @Resource(name = DruidConfiguration.DATASOURCE_NAME)
  private DataSource dataSource;

  @Bean(name = CUSTOM_PLATFORM_TRANSACTION_MANAGER_NAME)
  public PlatformTransactionManager platformTransactionManager() {
    return new DataSourceTransactionManager(this.dataSource);
  }


  /**
   * 配置事务拦截器,注意:transactionInterceptor 名称的 bean 可能已经存在,导致该生成 bean 的方法不能执行,所以定义成另外一个名字
   * @param platformTransactionManager 事务管理器
   * @return
   */
  @Bean(name = CUSTOM_TRANSACTION_INTERCEPTOR_NAME)
  public TransactionInterceptor customTransactionInterceptor(@Qualifier(CUSTOM_PLATFORM_TRANSACTION_MANAGER_NAME) PlatformTransactionManager platformTransactionManager) {

    NameMatchTransactionAttributeSource transactionAttributeSource = new NameMatchTransactionAttributeSource();
    RuleBasedTransactionAttribute       readOnly                   = this.readOnlyTransactionRule();
    RuleBasedTransactionAttribute       required                   = this.requiredTransactionRule();
    // 默认的只读事务配置

    for (String methodName : DEFAULT_READ_ONLY_METHOD_RULE_TRANSACTION_ATTRIBUTES) {
      transactionAttributeSource.addTransactionalMethod(methodName, readOnly);
    }

    // 默认的传播事务配置
    for (String methodName : DEFAULT_REQUIRED_METHOD_RULE_TRANSACTION_ATTRIBUTES) {
      transactionAttributeSource.addTransactionalMethod(methodName, required);
    }

    // 定制的只读事务配置
    for (String methodName : customReadOnlyMethodRuleTransactionAttributes) {
      transactionAttributeSource.addTransactionalMethod(methodName, readOnly);
    }

    // 定制的传播事务配置
    for (String methodName : customRequiredMethodRuleTransactionAttributes) {
      transactionAttributeSource.addTransactionalMethod(methodName, required);
    }

    return new TransactionInterceptor(platformTransactionManager, transactionAttributeSource);
  }

  /**
   * 当前存在事务就使用当前事务,当前不存在事务就创建一个新的事务 {@link org.springframework.transaction.annotation.Propagation#REQUIRED}
   */
  private RuleBasedTransactionAttribute requiredTransactionRule() {
    RuleBasedTransactionAttribute required = new RuleBasedTransactionAttribute();
    required.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
    required.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    required.setTimeout(TransactionDefinition.TIMEOUT_DEFAULT);

    return required;
  }

  /**
   * 只读事务 {@link org.springframework.transaction.annotation.Propagation#NOT_SUPPORTED}
   */
  private RuleBasedTransactionAttribute readOnlyTransactionRule() {
    RuleBasedTransactionAttribute readOnly = new RuleBasedTransactionAttribute();
    readOnly.setReadOnly(true);
    readOnly.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);

    return readOnly;
  }

  /**
   * 配置事务拦截
   * <p>
   * {@link #customTransactionInterceptor(PlatformTransactionManager)}
   */
  @Bean(name = CUSTOM_BEAN_NAME_AUTO_PROXY_CREATOR_NAME)
  public BeanNameAutoProxyCreator customTransactionBeanNameAutoProxyCreator() {
    BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
    // 设置定制的事务拦截器
    beanNameAutoProxyCreator.setInterceptorNames(CUSTOM_TRANSACTION_INTERCEPTOR_NAME);

    // 默认 + 定制
    String[] unions = ArrayUtils.addAll(DEFAULT_TRANSACTION_BEAN_NAMES, customTransactionBeanNames);
    beanNameAutoProxyCreator.setBeanNames(unions);
    beanNameAutoProxyCreator.setProxyTargetClass(true);

    return beanNameAutoProxyCreator;
  }
}

相关文章

  • SpringBoot 声明式事物配置

    特别注意的是定义 自定义事务拦截器 TransactionInterceptor bean 的名字不能使用 tra...

  • SpringBoot事物Transaction实战讲解教程

    SpringBoot事物Transaction实战讲解教程 - 虚无境 - 博客园 编程式事务管理 声明式事务...

  • spring中的事务管理

    Spring对事务的解决办法其实分为2种:编程式实现事务,AOP配置声明式解决方案。 声明式事物 1)Spring...

  • SpringBoot动态代理配置说明

    SpringBoot动态代理配置说明 基于CGLIB的代理与基于JDK的动态代理实现的声明式事务的区别 CGLIB...

  • Spring声明式事物

    Spring的事物分为两种,编程式事物和声明式事务,平时使用声明式事务是最多的。那么只要是事物就存在隔离级别:事务...

  • Spring事务

    Spring事务使用 spring事物配置,声明式事务管理和基于@Transactional注解的使用spring...

  • 2019-08-23 SpringBoot分布式应用系列

    转自:SpringBoot应用系列文章 SpringBoot应用之分布式会话 SpringBoot应用之配置中心 ...

  • springboot注解

    @SpringBootApplication 声明让springboot进行自动配置,用于注解类 @SpringB...

  • spring 编程式事物&声明式事物

    jar包 jdbcTemplate基本使用 -----------------------------------...

  • spring04

    Spring JdbcTemplate学习 Spring 声明式事务 xml配置实现 Spring 声明式事务 注...

网友评论

      本文标题:SpringBoot 声明式事物配置

      本文链接:https://www.haomeiwen.com/subject/ndxxtqtx.html