springboot 事务管理

启动类使用@EnableTransactionManagement注解
在使用注解的方法或类上使用@Transactional注解

package com.atguigu.spzx.manager.config;

import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
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.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: TODO 全局事务配置
 */
@Configuration
@Aspect
public class TransactionManagerConfig {
    private final static Logger logger = LoggerFactory.getLogger(TransactionManagerConfig.class);
    /**
     * 配置方法过期时间,默认-1,永不超时,单位毫秒
     */
    private static final int AOP_TIME_OUT = 50000;
    /**
     * 配置切入点表达式 : 指定哪些包中的类使用事务
     */
    private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.manager.service.impl.*.*(..)))";
    //事务管理器
    @Resource
    private TransactionManager transactionManager;
    /**
     * 声明业务方法的事务属性
     */
    @Bean
    public TransactionInterceptor txAdvice(){
        /**
         * 这里配置只读事务
         * 查询方法, 只读事务,不做更新操作
         */
        RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
        readOnlyTx.setReadOnly(true);
        readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

        /**
         * 增、删、改 需要的事务
         * 必须带事务
         * 当前存在事务就使用当前事务,当前不存在事务,就开启一个新的事务
         */
        RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute();
        // 设置回滚规则:什么异常都需要回滚
        requiredTx.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
        // 当前存在事务就使用当前事务,当前不存在事务就创建一个新的事务
        requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        requiredTx.setTimeout(AOP_TIME_OUT);
        /**
         * 无事务地执行,挂起任何存在的事务
         */
        RuleBasedTransactionAttribute noTx = new RuleBasedTransactionAttribute();
        noTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);

        /**
         * 设置方法对应的事务
         */
        Map<String, TransactionAttribute> methodMap = new HashMap<>();
        // 可以提及事务或回滚事务的方法
        //只读事务
        methodMap.put("get*", readOnlyTx);
        methodMap.put("query*", readOnlyTx);
        methodMap.put("find*", readOnlyTx);
        methodMap.put("list*", readOnlyTx);
        methodMap.put("count*", readOnlyTx);
        methodMap.put("exist*", readOnlyTx);
        methodMap.put("search*", readOnlyTx);
        methodMap.put("fetch*", readOnlyTx);
        //写事务
        methodMap.put("add*", requiredTx);
        methodMap.put("save*", requiredTx);
        methodMap.put("insert*", requiredTx);
        methodMap.put("update*", requiredTx);
        methodMap.put("modify*", requiredTx);
        methodMap.put("delete*", requiredTx);
        methodMap.put("creat*", requiredTx);
        methodMap.put("edit*", requiredTx);
        methodMap.put("remove*", requiredTx);
        methodMap.put("repair*", requiredTx);
        methodMap.put("binding*", requiredTx);
        //无事务
        methodMap.put("noTx*", noTx);
        // 其他方法无事务,只读
        methodMap.put("*", readOnlyTx);

        //声明一个通过方法名字配置事务属性的对象
        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
        source.setNameMap(methodMap);

        //返回事务拦截器
        TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, source);
        return txAdvice;
    }

    @Bean(name = "txAdviceAdvisor")
    public Advisor txAdviceAdvisor(TransactionInterceptor txAdvice) {
        logger.info("===============================创建txAdviceAdvisor===================================");
        //配置事务切入点表达式
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
        //增强事务,关联切入点和事务属性
        return new DefaultPointcutAdvisor(pointcut, txAdvice);
    }
}

配置完成也需在启动类加注解

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值