seata 手动回滚全局事务

1、seata 使用全局事务的时候,有时需要手动回滚事务,不需要抛异常则使用下面处理

import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.core.model.GlobalStatus;
import io.seata.core.model.TransactionManager;
import io.seata.spring.annotation.GlobalTransactional;
import io.seata.tm.TransactionManagerHolder;
import io.seata.tm.api.GlobalTransaction;
import io.seata.tm.api.GlobalTransactionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class TestServiceImpl implements ITestService {

    @Autowired
    private ProductCollectionService productCollectionService;

    @Autowired
    private BaseUserServiceClient baseUserServiceClient;

    @GlobalTransactional
    @Override
    public void test() {

        String xid = RootContext.getXID();
        TransactionManager manager = TransactionManagerHolder.get();
        try {
            log.info("test 获取事务id {}", xid);

            ProductCollection collection = new ProductCollection();
            collection.setProductId(1L);
            collection.setUserId(1L);
            collection.setCreateTime(new Date());
            productCollectionService.save(collection);

            ResultBody resultBody = baseUserServiceClient.increaseUserScore(1L, 2, "2");

            log.info("test 开始回滚数据");
            GlobalStatus status = manager.rollback(xid);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                manager.rollback(xid);
            } catch (TransactionException transactionException) {
                transactionException.printStackTrace();
            }
        }
    }

    @Override
    public void test1() {

        GlobalTransaction globalTransaction = GlobalTransactionContext.getCurrentOrCreate();

        try {
            globalTransaction.begin(300000, "my_test_tx_group");

            log.info("test1 获取事务 {}", globalTransaction.getXid());

            ProductCollection collection = new ProductCollection();
            collection.setProductId(1L);
            collection.setUserId(1L);
            collection.setCreateTime(new Date());
            productCollectionService.save(collection);

            ResultBody resultBody = baseUserServiceClient.increaseUserScore(1L, 2, "2");

            log.info("test1 开始回滚数据");
            globalTransaction.rollback();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                log.info("test1 开始回滚数据");
                globalTransaction.rollback();
            } catch (TransactionException transactionException) {
                transactionException.printStackTrace();
            }
        }
    }

}

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值