wood burning stoves*
The moose likes EJB and other Java EE Technologies and the fly likes What is 2 phase-commit ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "What is 2 phase-commit ?" Watch "What is 2 phase-commit ?" New topic
Author

What is 2 phase-commit ?

Gurumurthy Ramamurthy
Ranch Hand

Joined: Feb 13, 2003
Posts: 272
Can someone explain 2 phase-commit ??

Thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

"2 phase commit" is a protocol for global transactions. A global transaction is one which happens over more than one distinct transactional resource. The "2 phase" part refers to the two distinct parts of the process, which work (roughly!) like this:
  • First, a coordination process starts and sends a prepare message to all the participants (the distinct transactional resources) - this message will include some sort of unique identifier. When they get the message, each of the resources do what they need to do to see if they can commit the transaction. If they can the respond with a vote commit message, or a vote abort message if they can't.
  • Second, the coordinator examines all the votes - if all the resources agree they can commit their specific transactions then a global commit message is send out, again with the unique ID. The resources now commit all their transactions.


  • In a J2EE app. you might use this sort of sequence of events to encapsulate a method which does a JDBC operation and some JMS stuff. Both the JDBC operation can fail and a JMS message can fail to be sent. If either fail inside an XA transaction (another common name for 2 phase commit) then the JDBC operation will be rolled back, and the JMS message will be removed.

    Hope that makes sense. (And I got it right!)


    JavaRanch FAQ HowToAskQuestionsOnJavaRanch
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: What is 2 phase-commit ?
     
    Similar Threads
    Hibernate Transaction
    two phase commit
    Transaction and 2 PC
    mock exam + MDB transaction
    Bi-directional application level synchronization