JTA for distributed transactions

If the applications should interact with multiple databases in a distributed environment at the same time, it is important to handle the persistent data communication to commit the database transaction. This is called multi-phase transaction. The XA transaction is the standard mechanism for distributed transactions. Each individual transaction in a distributed transaction (also termed global transaction) is termed as a transaction branch.

A money transaction is the best example for such distributed transactions. Let's say we want to transfer money from account A to account B. The amount should be deducted from account A, only then if can it be successfully credited in account B, and vice versa.

JDBC supports the distributed transaction based on the connection pooling and open XA standards, and the transaction is handled by Java Transaction API (JTA). Many standard vendors offer the XA transactions, including Oracle.

Key components that take part in XA functionality include XADataSource, XAConnection, XAResource, and Xid (transaction IDs).

When an application is establishing a connection to the database, it is initially made with NO_TXN mode; however, based on the operation it is executing, it can switch between either of the following modes:

  • NO_TXN: This means that no transaction is currently consuming this connection.
  • LOCAL_TXN: This means that a local transaction on a database is consuming this connection. Additionally, auto_commit could be disabled. In this mode, applications are not supposed to invoke the prepare, commit, rollback, forget, or end operations on an XAResource; if invoked, it can throw an XAException.
  • GLOBAL_TXN: This means that a global transaction for a distributed system is currently consuming the connection. In this mode, applications are not supposed to invoke the commit, rollback, rollback(Savepoint), setAutoCommit(true), or setSavepoint commands on a java.sql.Connection. Additionally, the applications are not supposed to invoke the OracleSetSavepoint or OracleRollback commands on an oracle.jdbc.OracleConnection; if invoked, it can throw an SQLException.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset