For distributed transactions, if all the resources involved support transaction operations, the two-phase commit protocol is usually used for processing. However, if the resources involved in distributed transactions do not support transaction operations, compensation must be adopted.
Two phases: preparation (pre-submission) and submission.
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:
| • |
The application calls 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.
Two-Phase Lock
The two-phase locking protocol is used to ensure transaction continuity. In this protocol, a transaction must request a lock before releasing the locked object.
This protocol can be divided into the following two phases:
Expanding (growing) Phase-this phase allows a transaction to initiate a new lock request, but does not allow unlocking requests.
Release Phase (shrinking phase)-This phase allows transactions to release lock requests in the extension phase, but does not allow any new lock requests.
Two-phase submission
In the database management system, to maintain data integrity, you must maintain the atomic properties of the transaction (Atomic processing), that is, several data operations in the same transaction must succeed or fail at the same time.
In distributed databases, many sub-transactions are distributed across many distributed database servers. To coordinate these sub-transactions, the distributed database management system needs to consider more factors. Before confirming a transaction, you must first confirm the completion of each sub-transaction; otherwise, the transaction must be abandoned. Similarly, if any sub-transaction action cannot be completed, the Transaction Manager initiates a rollback request and the transaction will be rolled back. This method of managing distributed transactions is called the two-phase commit protocol. Two-phase commit ensures the completion or cancellation of the entire transaction.
Main Application Scope:
Recovery of distributed transactions
Synchronize table replication