欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot2.x 全局事务配置

程序员文章站 2022-03-01 12:42:44
...

首先依旧还是在启动类上加上事务开启注解@EnableTransactionManagement
如下

@SpringBootApplication(scanBasePackages = "com.xxx")
@MapperScan("com.xxx.service.db.repository")
@EnableDiscoveryClient
@EnableFeignClients
@EnableTransactionManagement
public class MthCmsServiceApplication {

	public static void main(String[] args) {
		SpringApplication.run(MthCmsServiceApplication.class, args);
	}

}

AOP依赖:

<dependency>  
      <groupId>org.springframework.boot</groupId>  
     <artifactId>spring-boot-starter-aop</artifactId>  
</dependency> 

全局事务管理配置如下,复制粘贴即用,但是得注意一点,增强的方法命名必须服从下面配置的规范

package com.xxx.service.config;

import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import java.util.Collections;

/**
 * @description 全局事务管理
 */
@Aspect
@Configuration
public class CmsTransactionConfig {


    private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.xxx.service.web.service.*.*(..))";
    private static final String[] REQUIRED_RULE_TRANSACTION = {"insert*", "create*", "add*", "save*","modify*", "update*", "del*", "delete*"};
    private static final String[] READ_RULE_TRANSACTION = {"select*", "get*", "query*", "search*", "count*","detail*", "find*"};

    @Autowired
    private TransactionManager transactionManager;

    @Bean
    public TransactionInterceptor txAdvice() {
        RuleBasedTransactionAttribute REQUIRED = new RuleBasedTransactionAttribute();
        REQUIRED.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
        REQUIRED.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
        REQUIRED.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        RuleBasedTransactionAttribute READONLY = new RuleBasedTransactionAttribute();
        READONLY.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
        READONLY.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
        READONLY.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
        READONLY.setReadOnly(true);
        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();

        for (String s : REQUIRED_RULE_TRANSACTION) {
            source.addTransactionalMethod(s, REQUIRED);
        }

        for (String s : READ_RULE_TRANSACTION) {
            source.addTransactionalMethod(s, READONLY);
        }

        return new TransactionInterceptor(transactionManager, source);
    }

    @Bean
    public Advisor txAdviceAdvisor(TransactionInterceptor txAdvice) {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
        return new DefaultPointcutAdvisor(pointcut, txAdvice);
    }
}

总结
1、首先在使用事务之前,要在启动类里面引入注解 @EnableTransactionManagement
2、使用全局事务的时候,方法命名一定要在上述规范中,切勿出现奇葩命名