File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "What is 2 phase-commit ?" Watch "What is 2 phase-commit ?" New topic

What is 2 phase-commit ?

Gurumurthy Ramamurthy
Ranch Hand

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

Paul Sturrock

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
    I agree. Here's the link:
    subject: What is 2 phase-commit ?
    jQuery in Action, 3rd edition