Wednesday, 12 January 2011

Non-XA and XA Transactions

Previous blogs have talked about MDBs and their use of transactions. This blog expands on the idea of transactions looking at the difference between XA and none-XA transactions and why they’re around...
A non-XA transaction, in basic terms, is something that wraps around a single resource that interacts with your code to co-ordinate your program’s access to that resource. When your code has finished accessing the resource, then the transaction will ensure that your code’s changes are committed. Resources generally include things like databases and JMS queues / topics.
An XA transaction takes this idea one step further and is, in the most general terms, a "global transaction" that may span multiple resources.

An XA transaction uses a coordinating transaction manager to co-ordinate access to one or more databases (or other resources, like JMS) that are all involved in the single global transaction. Non-XA transactions don’t use a transaction coordinator, and the single resource is doing all its transaction work itself. Tthis is sometimes referred to as a local transaction.

Most stuff in the world uses non-XA transactions: a Servlet or EJB or plain old JDBC in a Java application talking to a single database. XA gets involved in larger systems with multiple resources: two or more databases, a database and a JMS connection, all of those plus JCA resource, all in a single transaction. In this scenario, you'll use an application server like Websphere or Weblogic or JBoss, which acts as the Transaction Manager, and your various resources (Oracle, IBM MQ JMS, whatever) acting as transaction resources. Your code can then update/delete/publish/whatever across the many resources. When you say "commit", the results are committed across all of the resources. When you say "rollback", everything is rolled back across all resources.

The Transaction Manager coordinates all of this using a protocol called Two Phase Commit (2PC); however, remember that this protocol also has to be supported by the individual resources.

For more details - see the JTA pages on java.sun.com.