Seata分布式事务失效,不生效(事务不回滚)的常见场景

2年前 (2022) 程序员胖胖胖虎阿
376 0 0

一、微服务没有正常获取XID

检查方法:

在每个微服务中调用方法 RootContext.getXID() 检查XID

例如,服务A调用了服务B和服务C

那么可以分别在服务A、服务B、服务C的事务方法中添加

===============服务A
@Service
public class ServiceAImpl implements IServiceA
{

    private static final Logger log = LoggerFactory.getLogger(ServiceAImpl.class);

    @Autowired
    private IServiceB serviceB;
    @Autowired
    private IServiceC serviceC;
    
    @Override
    @GlobalTransactional
    @Transactional
    public Boolean doA() {
        log.info("Seata全局事务id=================>{}",RootContext.getXID());
        //......
    }

}

===============服务B
@Service
public class ServiceBImpl implements IServiceB
{

    private static final Logger log = LoggerFactory.getLogger(ServiceBImpl.class);

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Boolean doB() {
        log.info("Seata全局事务id=================>{}",RootContext.getXID());
        //......
    }

}

===============服务C
@Service
public class ServiceCImpl implements IServiceC
{

    private static final Logger log = LoggerFactory.getLogger(ServiceCImpl.class);

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Boolean doC() {
        log.info("Seata全局事务id=================>{}",RootContext.getXID());
        //......
    }

}

(1)查看XID是否为null

(2)每个服务中的XID不一致

如果出现以上两种其中一种,基本可以确定是因为XID导致事务失效

解决办法:

(1)XID正常情况下是通过请求头上的参数传递给下游服务的,可以通过seta包的getModifyRequest方法下的headers.put打断点进行分析

(2)在调用方法中直接传参给下游服务

===============服务A
@Service
public class ServiceAImpl implements IServiceA
{

    private static final Logger log = LoggerFactory.getLogger(ServiceAImpl.class);

    @Autowired
    private IServiceB serviceB;
    @Autowired
    private IServiceC serviceC;
    
    @Override
    @GlobalTransactional
    @Transactional
    public Boolean doA() {
        String XID = RootContext.getXID();
        serviceB.doB(XID);
        serviceC.doC(XID);
        //......
    }

}

===============服务B
@Service
public class ServiceBImpl implements IServiceB
{

    private static final Logger log = LoggerFactory.getLogger(ServiceBImpl.class);

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Boolean doB(XID) {
        RootContext.bind(XID);
        //......
    }

}

===============服务C
@Service
public class ServiceCImpl implements IServiceC
{

    private static final Logger log = LoggerFactory.getLogger(ServiceCImpl.class);

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Boolean doC(XID) {
        RootContext.bind(XID);
        //......
    }

}

二、undo_log表有脏数据

当Seata没有正常结束时,每个服务对应数据库中的undo_log表和seata持久化数据库的brach_table、global_table、lock_table、undo_log表都有可能有脏数据没有正确删除,从而导致服务一直回滚,却不成功

解决办法:清除undo_log表以及seata持久化数据库的brach_table、global_table、lock_table、undo_log表中的脏数据

三、Fegin调用使用了Fallback降级或抛出的异常被全局处理

这种情况下属于seata服务发现不了下游服务抛出的异常,导致事务不会触发回滚

解决办法:

(1)通过 GlobalTransactionContext.reload(RootContext.getXID()).rollback() 进行手动回滚

===============服务A
@Service
public class ServiceAImpl implements IServiceA
{

    private static final Logger log = LoggerFactory.getLogger(ServiceAImpl.class);

    @Autowired
    private IServiceB serviceB;
    @Autowired
    private IServiceC serviceC;
    
    @Override
    @GlobalTransactional
    @Transactional
    public Boolean doA() {
        Integer bStatus = serviceB.doB();
        if(bStatus == 0){//在Mybatis中,返回值为0证明插入失败
            //手动回滚
            GlobalTransactionContext.reload(RootContext.getXID()).rollback();
            return false;
        }
        Integer cStatus = serviceC.doC();
        if(cStatus == 0){//在Mybatis中,返回值为0证明插入失败
            //手动回滚
            GlobalTransactionContext.reload(RootContext.getXID()).rollback();
            return false;
        }
        //......
    }

}

===============服务B
@Service
public class ServiceBImpl implements IServiceB
{

    private static final Logger log = LoggerFactory.getLogger(ServiceBImpl.class);

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Boolean doB() {
        //......
        Integer insertStatus = serviceBDAO.insert();
        return insertStatus;
    }

}

===============服务C
@Service
public class ServiceCImpl implements IServiceC
{

    private static final Logger log = LoggerFactory.getLogger(ServiceCImpl.class);

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Boolean doC() {
        //......
        Integer insertStatus = serviceCDAO.insert();
        return insertStatus;
    }

}

(2)通过切面类

详见官方文档

https://gitee.com/link?target=http%3A%2F%2Fseata.io%2Fzh-cn%2Fblog%2Fseata-spring-boot-aop-aspectj.html

相关文章

暂无评论

暂无评论...