全局异常处理导致seata分布式事务无法回滚问题

项目场景:

微服务场景下,配置了统一全局异常处理,导致seata在AT模式下无法正常回滚问题

问题描述:

假设服务A调用服务B, 服务B发生异常,由于全局异常处理的存在(@ControllerAdvice), seata 无法拦截到B服务的异常,从而导致分布式事务未生效

解决方案:

  • 程序代码各自判断RPC响应码是否正常,再抛出异常

  • RPC接口不配置全局异常

  • 利用AOP切面解决

代码实现:

本篇文章使用AOP切面解决

/**
 * 全局事务处理切面
 * @author enbei
 * @Date 2022/12/8
 */
@Aspect
@Component
@Slf4j
public class GlobalTransactionAop {

    @Pointcut("within(com.ddwl..*.controller..*)")
    public void controllerAspect() {}


   // @Before("serviceAspect() && !@within(javax.websocket.server.ServerEndpoint)")
    @Before("controllerAspect()")
    public void before(JoinPoint joinPoint) throws TransactionException {
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
       // GlobalTransaction tx = GlobalTransactionContext.getCurrentOrCreate();
        if (StrUtil.isNotEmpty(RootContext.getXID())){
            log.info("拦截到需要分布式事务的方法:{},分布式全局事务XID:{}", method.getName(),RootContext.getXID());
        }
    }

    /**
     * 用于解决全局异常处理导致的全局事务失效问题
     */
    //@AfterThrowing(throwing = "e", pointcut = "serviceAspect() && !@within(javax.websocket.server.ServerEndpoint)")
    @AfterThrowing(throwing = "e", pointcut = "controllerAspect()")
    public void doRecoveryActions(Throwable e) throws TransactionException {
        if (StrUtil.isNotEmpty(RootContext.getXID())) {
            log.info("分布式全局事务XID:{}", RootContext.getXID());
            GlobalTransactionContext.reload(RootContext.getXID()).rollback();
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值