springboot实现全局事务管理

前言

首先需要实现全局事务管理,你得知道事务的原理包括一些事务的基础概念
事务源码解析

代码演示

import org.apache.ibatis.transaction.TransactionFactory;
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.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
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;

/**
 * 全局事务
 * @author fh
 * @date 2022-08-11
 * @since 3.1.0
 */
@Aspect
@Configuration
@ConditionalOnClass({TransactionManager.class, TransactionFactory.class})
public class TransactionAdviceConfig {

    private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.abupdate..*.service.*.*(..))";

    @Lazy
    private TransactionManager transactionManager;

    /**
     * 事务拦截器
     * @return
     */
    @Bean
    public TransactionInterceptor txAdvice() {

        RuleBasedTransactionAttribute txAttrRequired = new RuleBasedTransactionAttribute();
        //支持一个已经存在的事务。如果没有事务,则开始一个新的事务;
        txAttrRequired.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        txAttrRequired.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
        //设置只读
        RuleBasedTransactionAttribute txAttrRequiredReadonly = new RuleBasedTransactionAttribute();
        txAttrRequiredReadonly.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        txAttrRequiredReadonly.setReadOnly(true);

        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
        //可以提交事务或回滚事务的方法
        source.addTransactionalMethod("insert*", txAttrRequired);
        source.addTransactionalMethod("save*", txAttrRequired);
        source.addTransactionalMethod("add*", txAttrRequired);
        source.addTransactionalMethod("create*", txAttrRequired);
        source.addTransactionalMethod("delete*", txAttrRequired);
        source.addTransactionalMethod("remove*", txAttrRequired);
        source.addTransactionalMethod("update*", txAttrRequired);
        source.addTransactionalMethod("modify*", txAttrRequired);
        source.addTransactionalMethod("exec*", txAttrRequired);
        source.addTransactionalMethod("set*", txAttrRequired);
        source.addTransactionalMethod("reset*", txAttrRequired);
        source.addTransactionalMethod("batch*", txAttrRequired);
        source.addTransactionalMethod("lock*", txAttrRequired);
        source.addTransactionalMethod("register*", txAttrRequired);
        //其他方法无事务,只读
        source.addTransactionalMethod("*", txAttrRequiredReadonly);

        return new TransactionInterceptor(transactionManager, source);
    }

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

}

注意

关于事务全局管理如何使用:就是你如果需要对某个业务方法进行事务管理纳入全局的话,首先方法名需要满足上面几个前缀,否则只能归类韦其他方法无事务,只读
source.addTransactionalMethod(“*”, txAttrRequiredReadonly);

你可能感兴趣的:(springboot,#,java-动态代理,事务管理,spring,boot,AOP,拦截器,事务)