2フェーズコミットを理解しようとしていますが、各ローカルサイトが分散トランザクションの一部をいつ実行するかがわかりません。
これは、準備メッセージが送信される前に発生しますか。つまり、2フェーズコミットxaプロトコルが実行される前に発生しますか?
または、各サイトは準備メッセージを受信した後に分散トランザクションの一部を実行しますか?つまり、準備メッセージ自体にも実行されるトランザクションクエリが含まれていますか?
2フェーズコミットを理解しようとしていますが、各ローカルサイトが分散トランザクションの一部をいつ実行するかがわかりません。
これは、準備メッセージが送信される前に発生しますか。つまり、2フェーズコミットxaプロトコルが実行される前に発生しますか?
または、各サイトは準備メッセージを受信した後に分散トランザクションの一部を実行しますか?つまり、準備メッセージ自体にも実行されるトランザクションクエリが含まれていますか?
はい、実行は準備メッセージが送信される前に行われます。すべてがすでに実行された後、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!
実際、ご覧のとおり、準備メッセージは、コーディネーターによって、そのようなトランザクションのコンテキスト内で以前に何かを実行したため、そのトランザクションの参加者として以前に登録したサイトにのみ送信されます。