Show TOC

JTA and Local TransactionsLocate this document in the navigation structure

Use

Transactions are one of the most important concepts of the persistence layer. Their correct usage guarantees the consistency of the underlying data, as well as the performance of your application. Therefore, it is important to know what types of transactions you can use in your application components, and how to use them.

The application components that you deploy on AS Java may deal with two basic types of transactions: JTA transactions and local transactions.

JTA Transactions

These are transactions defined by the Java Transaction API (JTA) standard, therefore they are referred to as “JTA transactions” .

The following list outlines the most important characteristics of a JTA transaction, which distinguishes it from a local transaction:

  • JTA transactions are managed by a transaction manager, which is external to the data store. In AS Java, it is a part of the Transaction Service.

  • Such transactions are distributed, that is, they may involve communication with a number of resource managers. For example, an application may write data to several data stores in the branches of a single distributed transaction. The consistency of data, which is one of the main requirements for a transaction, is ensured because eventually either all branches of the transaction are committed (the changes they do are stored in the data store) or all of them are rolled back (the initial state of the data is restored). This is achieved using the two-phase commit (2PC) protocol.

    More information:

    Distributed Transactions

    Two-Phase Commit

  • JTA transactions use the javax.transaction.xa.XAResource interface, which enables the coordination of the transaction branches.

  • JTA transactions are either container- or component-driven. You can demarcate a JTA transaction in certain application components - session and message-driven beans with bean-managed transaction demarcation, and Web components, or leave the transaction demarcation to the EJB Container. It is better to use container-managed transactions, because this makes your code more simple, less error-prone, and easier to debug.

  • JTA is the Java EE standard for transaction management. It is the most natural way for a Java EE application to demarcate its transactions. Moreover, using standard JTA transactions ensures the portability of your application.

Recommendation

Use JTA transactions in your applications. They provide more flexibility than local transactions by enabling work in a distributed environment.

Local Transactions

The main characteristics of local transactions are the following:

  • Unlike JTA transactions, local transactions work with a single resource manager, which manages the transactions.

  • You can code local transaction demarcation in your application, but you must use local transactions carefully. You may choose to use local transactions in a simple scenario, which does not require sophisticated transaction management.