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 transaction is like global transaction.
- 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.
- Transaction manager (TM) is responsible for bringing multiple resources under a single transaction.
- Transaction manger is the capability of the container.
- TM uses 2PC (Two phase commit) pattern for committing the transaction.
- The resources (JMS, DB etc ) involved in the transaction should have capability to support 2PC pattern otherwise XA will not work.
- Non-XA transaction is like a local truncation.
- Use this approach when a single resource is doing all it’s truncation work itself.
What is 2PC ( TWO Phase commit ) Protocol?
- 2PC is used to make sure the transactions are in sync when you have 2 or more DBs.
- Assume I have two databases ( DB-1 and DB-2) using 2PC, both of these databases are in two different locations.
- 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.