Geeks With Blogs
Pradeep Loganathan Distributed

To understand how a two phase commit works we need to understand all the actors involved in a two phase commit

 

  1. Resource Manager: A resource manager manages either a durable resource such as a database, (e.g. SQL Server), a queuing system (e.g.  MSMQ) or a volatile resource (e.g. in-memory transacted hash table) and provides facilities for the transaction manager for resource enlistment, delistment e.tc. A
  2. Transaction Manager: The transaction manager provides facilities for an application to initiate transactions and coordinates with all the resource managers involved in the transaction.

 

The transaction manager is the root of the transaction and coordinated between all the resource managers involved in the transactions. Now let’s see the phases and the steps involved in a two phase commit in a happy path transaction (i.e. everything commits).

 

  1. Commit-request phase:
    1. The TM asks all the resource managers to prepare for the transaction.
    2. All the RM’s prepare the transaction, maybe hold locks on the changes and record the entries in the respective redo and undo logs.
    3. All the RM’s respond back to the TM with a prepared message.
  2. Commit phase
    1. If all the RM’s responded with a prepared message then the TM writes a commit record to the commit log.
    2. The TM sends a commit message to all the RM’s.
    3. Each RM then commits the changes and releases all locks held.
    4. Each RM then sends a committed message to the TM.
    5. The TM writes a committed record to its log.
    6. The TM completes the transaction when all the RM’s have responded with a committed message.

 

Now let’s see the second scenario when a RM fails to make the changes durable as a result of which the transaction needs to be rolled back.

 

a.       A RM responds with an abort message.

b.      The TM sends a Rollback message to all the RM’s.

c.       Each RM then rollsback the transaction using the undo logs created in thye commit-request phase and releases any locks held.

d.      The TM completes the transaction when all the RM’s have responded with a rollback

 

Posted on Thursday, February 9, 2006 9:04 AM Transactions | Back to top

Copyright © Pradeep Loganathan | Powered by: GeeksWithBlogs.net