Show TOC

Background documentationTwo-Phase Commit Locate this document in the navigation structure

 

Purpose

The purpose of the two-phase commit (2PC) protocol is to ensure the atomicity of distributed transactions. These are transactions that work with more than one resource manager, and that must synchronize their branches when they finish their work.

Example Example

If the transaction works with five resource managers and one of them fails while the others commit, the failed one is not able to commit later and the atomicity of the transaction is broken. This also affects the integrity of data since the changes are not saved in all resource managers.

End of the example.
Process Flow

In the 2PC scenario, there is one coordinator branch that sends a request to all other branches of the transaction to prepare to commit. The coordinator is the server node that starts the distributed transaction.

In this first phase, the coordinator must obtain a positive reply from all branches to send a message for the transaction commitment. If all branches return a positive reply, the transaction commits (graphic 1). If there is at least one branch that returns a negative reply or unknown status, the system rolls back the whole transaction (graphic 2).

In the second phase the coordinator sends a message to all branches to either commit or rollback. If any of the branches fail to commit, the whole transaction rolls back.

Note Note

The acknowledgments sent by the resource managers to confirm the execution of the transaction branches do not affect the performance of the system. The performance depends on how fast the preparation and the commitment or rollback of the branches is executed.

End of the note.

This graphic is explained in the accompanying text.

Graphic 1: Commit Scenario

This graphic is explained in the accompanying text.

Graphic 2: Rollback Scenario

More Information

Distributed Transactions