NOTE
2.9 Two-Phase Implementation: 2PC
1. What Is 2PC - 2PC: Two-Phase Commit. - Divide the transaction into two phases. - In the first phase, the transaction manager sends prepare requests to all databases. - If all respond ok, execute commit in the second phase; if any responds fail, execute rollback in the second phase.
This is a historical learning note and may contain outdated or incomplete understanding.
1. What Is 2PC?
- 2PC: Two-Phase Commit.
- Divide the transaction into two phases:
- In the first phase, the transaction manager sends prepare requests to all databases.
- If all respond ok, execute commit in the second phase; if any responds fail, execute rollback in the second phase.

2. Problems with 2PC
- Single-point problem: if the transaction manager or any resource manager goes down, the entire transaction cannot execute.
- Synchronous blocking: each database starts locking resources in the first phase and does not release the locked resources until the second phase, remaining blocked the whole time.
3. Implementations of 2PC
3.1. XA
- The DTP model defines several roles:
- AP: our microservice.
- TM: global transaction manager.
- RM: database.
- CRM: communication middleware between TM and RM.
- A distributed transaction can be split into many local transactions running on different APs and RMs.
- The ACID properties of each local transaction are easy to implement, but the global transaction must ensure that every local transaction it contains can succeed at the same time. If one local transaction fails, all other transactions must be rolled back. The problem is that while a local transaction is being processed, it does not know the running state of the other transactions. Therefore, CRM is needed to notify each local transaction and synchronize transaction-execution status.
- Therefore, communication among local transactions must follow a unified standard, otherwise different databases cannot communicate. XA is the interface specification for communication between TM and RM.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub