XA, Non-XA transaction and 2PC Protocol

Wisely choose transaction type while designing your application. Transaction, a key aspect of any application should be taken care well in advance. Here are few tips while choosing between XA and Non-XA transaction.

XA transactions
  1. XA transaction is like global transaction.
  2. When you have multiple resources involved for a single transaction, you should be using XA transaction.Multiple resource means, 2 or more databases or JMS listener and a database etc.
  3. Transaction manager (TM) is responsible for bringing multiple resources under a single transaction.
  4. Transaction manger is the capability of the container.
  5. TM uses 2PC (Two phase commit) pattern for committing the transaction.
  6. The resources (JMS, DB etc ) involved in the transaction should have capability to support 2PC pattern otherwise XA will not work.

 NON-XA transaction

  1. Non-XA transaction is like a local transaction.
  2. Use this approach when a single resource is doing all it’s transaction work itself.
What is 2PC ( TWO Phase commit ) Protocol?
  1. 2PC is used to make sure the transactions are in sync when you have 2 or more DBs.
  2. Assume I have two databases ( DB-1 and DB-2) using 2PC, both of these databases are in two different locations.
  3. Now, before DB-1 and DB-2 are ready to commit transactions, they will notify transaction manager that they are ready to commit ( PHASE 1 ). So when the transaction manager is acknowledge, it will send a signal to DB-1 and DB-2 telling them go ahead for commit (PHASE – 2 )..
Important note-> Two phase commit does not guarantee that a distributed transaction can't fail, but it does guarantee that it can't fail silently without the TM being aware of it.


No comments: