Overview of two-phase commit protocol
Two-phase commit protocol is comprised of a set of actions that ensure a transaction involving multiple databases does not produce unsynchronized updates.
Two-phase commit provides a way for a series of database interactions on multiple different data sources to be grouped together and completed or rolled back as a single transaction. Two-phase commit transactions that represent a series of database interactions on multiple data sources are referred to as global transactions.
At the beginning of a global transaction, a global transaction ID (XID) is generated and used by an external transaction manager to drive the two-phase commit processing across all of the resource managers involved. Each database interaction within the scope of the global transaction is executed upon its associated resource manager. The results of the interactions are then sent back to the application for processing. When the database interactions within the scope of the global transaction are finished, a prepare call is sent to each resource manager that was accessed by the global transaction. The prepare call provides each resource manager a chance to determine and report on its ability to commit the work it has done as a part of the global transaction. Upon receiving verification that each resource manager can commit its work, the transaction manager sends a commit call to each resource manager.
At any point in time prior to sending the commit call, the two-phase commit transaction can be rolled back. If the transaction is rolled back, a rollback call is sent to each resource manager involved in the transaction and the temporary changes are removed or discarded.
If any database failures occur during the commit phase, the external transaction manager tries to reestablish a connection with the failed resource manager and resumes its call for the resource manager to commit.
If the transaction manager fails during the commit phase, the transaction manager performs recovery processing upon restart and attempts to reestablish a connection with all of the resource managers involved. When the transaction managers reestablishes the connections, it resumes its call for the resource managers to commit.