# how two phase commit ensure serializability

Two phase commit is used in distributed transaction. For example, a client sends a transaction to two databases with a coordinator.

• step1: client get a global transaction id from coordinator
• step2: client send the transaction to two databases respectively
• step3: client send the commit flag to coordinator
• step4: coordinator send prepare flag to two databases, and two databases response prepare ack
• step5: coordinator send commit flag to two databases, and two databases response commit ack
• step6: coordinator response commit ack to client

My questions are

• What is the global transaction id in step1 used for?
• And two phase commit is used to ensure the atomic in ACID, but how does it ensure the serializability? For examples, if client A sends transaction A and client B sends transaction B to databases simutaniously, then two databases may execute two transactions in different orders. Then two databases may end with non consistent states.

Let me try to explain this using an example. Suppose there is coordinator and two participants named P1 & P2. Now suppose both P1 & P2 creates a transaction to modify a variable A and no other transaction is raised in between, in this case coordinator know that both participants are doing same this coordinator will raise a commit message indicating that both participants can commit and changes will be saved.
Now let's take other example Suppose variable A depends on variable B, now participant P1 raises message to modify variable A and at the same time participant P2 raises message to modify both B & A, in this case coordinator know that participants are going out of sync by executing these commands so master will decline this transaction since it will not serialize.