开发者社区 > 数据库 > 关系型数据库 > 正文

shardingsphere整合seata时出现本地事务失效

shardingsphere5.5整合seata2.0.0及dubbo3.3.0执行业务时出现本地事务失效的情况,代码如下:
@GlobalTransactional(rollbackFor = Exception.class)
public int insertMember( int x) throws TransactionException {

    int i = 0;

    try {
        // 通过dubbo远程调用
        i = remoteInsertMember();
        System.err.println("远程调用返回结果为" + i);
        if (RootContext.inGlobalTransaction()) {
            System.err.println("远程调用后在全局事务中" + RootContext.getXID());
        } else {
            System.err.println("远程调用后不在全局事务中" + RootContext.getXID());
        }
        // 运行到此处时尚在全局事务中
        // 调用本地方法
        i = localInsertMember();
        System.err.println("本地调用返回结果为" + i);
        if (RootContext.inGlobalTransaction()) {
            System.err.println("本地调用后在全局事务中" + RootContext.getXID());
        } else {
            System.err.println("本地调用后不在全局事务中" + RootContext.getXID());
        }
        // 运行到此处时已不在全局事务中RootContext.getXID()为null
        int aaa = 10 / x;

    } catch (Exception e) {
        System.err.println("执行失败,回滚事务" + e.getMessage());
        System.err.println("回滚时RootContext.getXID()=" + RootContext.getXID());  // 运行到此处时已不在全局事务中RootContext.getXID()为null
        if (RootContext.inGlobalTransaction()) {
            System.err.println("回滚时在全局事务中" + RootContext.getXID());
        } else {
            System.err.println("回滚时不在全局事务中" + RootContext.getXID());
        }
        GlobalTransactionContext.reload(RootContext.getXID()).rollback();
    }
    return i;
}

展开
收起
dqwum42ruvqek 2024-10-04 15:44:16 12 0
0 条回答
写回答
取消 提交回答

阿里云关系型数据库主要有以下几种:RDS MySQL版、RDS PostgreSQL 版、RDS SQL Server 版、PolarDB MySQL版、PolarDB PostgreSQL 版、PolarDB分布式版 。

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载