Pregunta

I am looking at the CouchBase Dev Guide and trying to understand how Two Phase Commits work. I feel like the code example they give differs from the diagram.

Code Example

They link to a Ruby Gist that describes how you would do this transferring points from one account to another.

My understanding is that they go down the following route:

  1. Change transaction state to pending
  2. Update the first account by altering points and adding reference to transaction
  3. Update the second account by altering points and adding reference to transaction
  4. Change transaction state to committed
  5. Remove reference of transaction from first account
  6. Remove reference of transaction from second account
  7. Change the transaction state to done

In this example, if there was a failure between steps 2 and 3, we could rollback by reversing the changed to points to any account which has a reference to a transaction.

Diagram Example

This is the diagram given to explain two phase locking; I think it disagrees with the code example...

Two Stage Commit Diagram

The diagram seems to say that you add a reference to transaction to both accounts, then you add/remove points from both accounts.

In this example, if there was a failure between steps 3 and 4, how would you know what to roll back? How would you know if you had applied the changed to points or not?

Is the diagram wrong?

¿Fue útil?

Solución

Yeah, you are right. The diagram step 3 should also apply balance changes at the same time as adding transaction to the list. Basically adding transaction to the list is just a sign that some changes were applied to the balance.

btw in the link to original gist you can find more complete executable solution, with rollback code: https://gist.github.com/avsej/3136027

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top