Jakarta Transactions
The Jakarta Transactions, one of the Jakarta EE APIs, enables distributed transactions to be done across multiple X/Open XA resources in a Java environment. JTA was a specification developed under the Java Community Process as JSR 907. JTA provides for:
- demarcation of transaction boundaries
- X/Open XA API allowing resources to participate in transactions.
X/Open XA architecture
JTA implementation of the X/Open XA architecture
The JTA API consists of classes in two Java packages:The JTA architecture requires that each resource manager must implement the interface in order to be managed by the TP monitor. As stated previously, each resource will have its own specific API, for instance:
- relational databases use JDBC
- messaging services use JMS
- generalized EIS resources use Java EE Connector API.
API
UserTransaction interface
The interface provides the application theability to control transaction boundaries programmatically. This interface may be used
by Java client programs or EJB beans.
The method starts a global transaction and associates the
transaction with the calling thread. The transaction-to-thread association is managed
transparently by the Transaction Manager.
Support for nested transactions is not required. The UserTransaction.begin method
throws the NotSupportedException when the calling thread is already associated
with a transaction and the transaction manager implementation does not support nested
transactions.
Transaction context propagation between application programs is provided by the
underlying transaction manager implementations on the client and server machines.
The transaction context format used for propagation is protocol dependent and must be
negotiated between the client and server hosts. For example, if the transaction manager
is an implementation of the JTS specification, it will use the transaction context
propagation format as specified in the CORBA OTS 1.1 specification. Transaction
propagation is transparent to application programs.
@Transactional annotation
The annotation provides the application theability to control transaction boundaries declaratively. This annotation can be applied to any class that the Jakarta EE specification
defines as a managed bean.
The code sample below illustrates the usage of in a request scoped CDI managed bean:
@RequestScoped
public class ExampleBean
Transactional behavior can be configured via an attribute on the annotation. The available options closely mirror those of the EJB specification.
@TransactionScoped annotation
The annotation provides the application theability to declare that the scope during which a bean lives is tied to the time a given transaction is active.
The code sample below illustrates the usage of in a request scoped CDI managed bean:
@TransactionScoped
public class TxScopedBean
@RequestScoped
public class ExampleBean
If method foo is first called on a managed instance of ExampleBean and then subsequently method bar is called, the number printed will be 0 and not 1. This is because each method had its own transaction and therefore its own instance of TxScopedBean. The number 1 that was set during the call to foo will therefore not be seen during the call to bar.
UserTransaction support in EJB server
servers are required to support the interface for use by EJBbeans with the BEAN value in the annotation. The
interface is exposed to EJB components through either the interface using the
method, or directly via injection using the general
@Resource annotation. Thus, an EJB application does not interface with theTransaction Manager directly for transaction demarcation; instead, the EJB bean relies
on the EJB server to provide support for all of its transaction work as defined in the
Jakarta Enterprise Beans Specification.
The code sample below illustrates the usage of via bean-managed transactions in an EJB session bean:
@Stateless
@TransactionManagement
public class ExampleBean
Alternatively, the can be obtained from the :
@Stateless
@TransactionManagement
public class ExampleBean
Note though that in the example above if the
@TransactionManagement annotation is omitted, a JTA transaction is automatically started whenever foo is called and is automatically committed or rolled back when foo is exited. Making use of a is thus not necessary in EJB programming, but might be needed for very specialized code.UserTransaction support in JNDI
The UserTransaction should be available underjava:comp/UserTransaction.