Reprinted: http://hi.baidu.com/liuhann/item/d644cfd873c904ffcb0c39b1
Two-phase submission Protocol
The key to implementing distributed transactions is the two-phase commit protocol. In this Protocol, one or more Resource Manager activities are controlled by a separate software component called the Transaction Coordinator. The five steps in this Protocol are as follows:
•
ApplicationProgramCall the commit method in the Transaction Coordinator.
•
The transaction Coordinator contacts each resource manager involved in the transaction and notifies them to prepare for committing the transaction (this is the beginning of the first phase ).
•
To respond to the preparation phase in a positive way, the resource manager must put itself in the following status: ensure that the transaction can be committed when the transaction is requested, or roll back the transaction when the transaction is required to be rolled back. Most resource managers write the log files (or equivalent files) that they plan to change to the persistent storage. If the resource manager cannot prepare a transaction, it will respond to the Transaction Coordinator with a negative response.
•
The transaction Coordinator collects all responses from the resource manager.
•
In the second stage, the Transaction Coordinator notifies each resource manager of the transaction result. If any resource manager makes a negative response, the Transaction Coordinator sends a rollback command to all resource managers involved in the transaction. If the resource manager responds positively, the Transaction Coordinator instructs all resource managers to submit transactions. Once the resource manager is notified to submit, subsequent transactions cannot fail. By responding to the first stage in a positive way, each resource manager has ensured that the transaction will not fail if it is notified to commit the transaction later.
Figure 1 and figure 2 use two sequence diagrams to describe the two-phase commit protocol.
Figure 1 transaction commit
Figure 1 shows the transaction succeeded (committed ). Figure 2 shows the two-phase commit protocol when one of the resource managers cannot submit for some reason.