Two-phase commit

From Maths
Jump to: navigation, search

Overview

To make an atomic action a commit operation needs to either succeed in its entirety or fail and leave the state unaltered. This is fairly easy to "single" things, however in general what if you want multiple things to do a commit (or a rollback). The problem is as follows:

  • Suppose there are 3 actions that we wish to commit "atomically", [ilmath]A[/ilmath], [ilmath]B[/ilmath] and [ilmath]C[/ilmath]
  • Suppose [ilmath]A[/ilmath] has committed, but [ilmath]B[/ilmath] failed to commit. You can no longer rollback [ilmath]A[/ilmath] (without breaking atomicity, as [ilmath]A[/ilmath] has committed! What should you do with [ilmath]B[/ilmath] and [ilmath]C[/ilmath] now? [ilmath]B[/ilmath] cannot happen, so this situation is already "broken".

Two-phase commit

If each action supports the following methods:

  • prepareToCommit (even if this succeeds, we may still rollback)
  • actuallyCommit (with a "no-throw guarantee" - this cannot fail, if prepareToCommit has succeeded)

(rollback, and locks here - saving page to save work)