Java Reference
In-Depth Information
6.1.6. Two-phase commit
The two-phase commit protocol is used in situations where a transaction spans multiple re-
sources. As its name suggests, a two-phase commit has two phases. In the first phase the
transaction manager polls the resource managers asking them if they're ready to commit. If
all of the resource managers reply affirmatively, the transaction manager then issues a com-
mit message to each resource manager, as shown in figure 6.3 . If any one of the resource
managers responds negatively, the transaction is rolled back. Each resource manager is re-
sponsible for maintaining a transaction log so that if anything happens, the transaction can
be recovered.
Figure 6.3. Two-phase commit protocol. In phase 1, each database reports back to the transaction manager that
the changes can be persisted. In phase 2, the transaction manager tells the individual databases to commit.
You might be wondering if a resource manager could vote to commit but then subsequently
fail. The answer is yes; this can and does happen for a variety of reasons. Between the
time that the data is polled and the commit is issued, a shutdown request might be issued
to the database or a network connection might be lost. Often, though, a time-out might be
exceeded due to high load and the transaction manager makes a unilateral decision to roll
back the transaction. In this case a heuristic exception is thrown.
There are three different heuristic exceptions that could be thrown: HeuristicCom-
mitException , HeuristicMixedException , and HeuristicRollbackEx-
ception . A HeuristicCommitException is thrown when a request is issued to roll
back but a heuristic decision is made to commit. A HeuristicMixedException is
thrown when some resource managers commit but others fail. In this situation the system is
 
Search WWH ::




Custom Search