seata源码-全局事务提交 服务端源码

前面的博客中,我们介绍了,发起全局事务时,是如何进行全局事务提交的,这篇博客,主要记录,在seata分布式事务中,全局事务提交的时候,服务端是如何进行处理的

发起全局事务提交操作

事务发起者,在所有分支事务执行完毕之后,如果没有发生异常,会进行全局事务提交

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

这里就不做过多解释了,前面seata入门和全局事务begin的博客中,有介绍过这里入参的request对象的重要性

服务端接收到请求

前面全局事务begin的源码,介绍过,netty服务端接收到请求之后,是如何执行到这里的,在这里会根据request请求的类型,交给不同的handler来处理
在这里插入图片描述

io.seata.server.AbstractTCInboundHandler#handle(io.seata.core.protocol.transaction.GlobalCommitRequest, io.seata.core.rpc.RpcContext)

在这里插入图片描述

io.seata.server.coordinator.DefaultCoordinator#doGlobalCommit

在这里插入图片描述

上面也没有太多的业务逻辑,没什么好说的,我们直接来看core.commit()方法的逻辑

io.seata.server.coordinator.DefaultCore#commit


@Override
public GlobalStatus commit(String xid) throws TransactionException {
   
    /**
     * 1.获取全局session:globalSession
     */
    GlobalSession globalSession = SessionHolder.findGlobalSession(xid);
    if (globalSession == null) {
   
        return GlobalStatus.Finished;
    }
    /**
     * 2.给globalSession添加监听
     */
    globalSession.addSessionLifecycleListener(SessionHolder.getRootSessionManager());
    // just lock changeStatus
    /**
     * 3.对globalSession进行一些处理
     */
    boolean shouldCommit = SessionHolder.lockAndExecute(globalSession, () -> {
   
        // Highlight: Firstly, close the session, then no more branch can be registered.
        /**
         * 3.1 globalSession.setActive(false);将全局session的active设置为false
         * 在clean()方法中会把lockTable中本次加锁的记录(分支事务相关锁信息)删除
         */
        globalSession.closeAndClean();
        if (globalSession.getStatus() == GlobalStatus.Begin) {
   
            /**
             * 对于AT模式,这里永远返回false
             * 对于AT模式,这里会执行if的逻辑,将globalSession的status设置为AsyncCommitting
             */
            if (globalSession.canBeCommittedAsync()) {
   
                globalSession.asyncCommit();
                return false;
            } else {
   
                /**
                 * 将globalSession的状态设置为committing状态
                 */
                globalSession.changeStatus(GlobalStatus.Committing);
                return true;
            }
        }
        return false;
    });

    /**
     * 4.通知分支事务进行提交,如果是AT模式,不会进入到这里执行,因为shouldCommit是false
     * 是通过一个异步线程来进行调用doGlobalCommit()方法的
     */
    if (shouldCommit) {
   
        boolean success = doGlobalCommit(globalSession, false);
        //If successful and all remaining branches can be committed asynchronously, do async commit.
        if (success && globalSession.hasBranch(
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值