1

我试图理解两阶段提交,但我不清楚每个本地站点何时执行其分布式事务的一部分。

在发送准备消息之前是否发生这种情况。那是在两阶段提交 xa 协议甚至运行之前发生吗?

还是每个站点在收到准备消息后执行其部分分布式事务,这意味着准备消息本身也包括要执行的事务查询?

4

1 回答 1

2

是的,在发送准备消息之前执行。您应该假设在所有内容都已执行后,整个 2PC 协议在 commit() 操作中运行。考虑以下最终提交的分布式事务的想象轨迹。缩进意味着过程嵌套:

transactionalOperation() is a procedure at some client site
    begin() is a remote invocation to the coordinator
        create global XID
        return global XID
    XID is added to the thread-local context at the caller
    executeOperation(params) is a remote invocation to a participant site
        XID is implicitly propagated to the server side thread
        inform coordinator that this site is a participant
        execute the operation! (acquiring locks)
    ... more operations ....
    commit() is a remote invocation to the coordinator
        XID is implicitly propagated to the server side thread
        -------------- 2PC starts here ----------------
        for all registered participants:
            prepare(XID) is a remote invocation to each site
                make sure that the transaction can be committed,
                usually by writing and flushing a log 
                return OK
        gather decisions
        for all reguistered participants:
            commit(XID) is a remote invocation to each site
                log transaction commit
                (release locks)
        -------------- 2PC ends here ----------------
    XID is removed from the thread-local context at the caller
    transaction is complete!

实际上,正如您所看到的,准备消息将由协调器仅发送到那些先前在此类事务的上下文中执行过某些操作的站点,因此先前已注册为该事务的参与者。

于 2012-11-17T16:38:49.647 回答