Java Transaction API (JTA) provides a set of APIs for implementing the transaction service in a client as well as a bean. From Figure 14.3, you can see the set of interfaces and components in the transaction architecture. JTA broadly consists of three elements:
Figure 14.3. JTA transaction architecture.A JTA transaction is available for all J2EE applications, like JSPs, servlets, and EJBs. Next you will learn about the different classes and interfaces that form part of the Java Transaction API. The Java Transaction APIs include two packages: javax.transaction and javax.transaction.xa. The javax.transaction package includes these interfaces:
The javax.transaction.xa package includes these interfaces:
The details of these interfaces are discussed in the following sections. UserTransaction InterfaceThe UserTransaction interface is meant for client applications, or within an EJB, is meant as a part of a bean-managed transaction, to set the transaction boundaries. It manages the transaction associated with the current thread. It provides methods for beginning and ending the transactions, namely, begin(), commit(), and rollback(), it sets the transaction for rollback setRollbackOnly(), gets the status of the transaction getStatus(), and sets the transaction timeout setTransactionTimeout(). The following are details of a few important methods:
TransactionManager InterfaceThe TransactionManager interface allows an application server to communicate with the transaction manager, and to provide transaction demarcation, propagation, and resource management on behalf of an application. It provides methods for setting transaction boundaries such as begin(), commit(), rollback(), suspend(), and resume(), sets the transaction for rollback setRollbackOnly(), gets the status of the transaction (getStatus()), and sets the transaction timeout setTransactionTimeout(). Details of a few important methods follow:
Transaction InterfaceEvery global transaction is associated with this interface. This interface provides methods for transaction demarcation, such as commit() and rollback(), resource enlisting enlistResource(), which is used when a transaction starts, and delisting delistResource() for when a transaction ends. The details of two important methods are
Synchronization InterfaceThe synchronization interface allows the application to be notified by the transaction manager before and after the completion of a global transaction. The beforeCompletion() and afterCompletion() methods are called before starting and after completing a global transaction. XAResource InterfaceThis interface is a Java mapping of the X/Open XA protocol. It provides a bridge of communication between the resource manager and the transaction manager. Assume, for example, that an application accesses more than one database. Each database connection will be enlisted with the transaction manager. The transaction manager will then obtain the XAResource for each of the databases participating in the transaction. The transaction manager uses the start() method to associate these resources with the transaction and then uses the end() method to disassociate the resources from the transaction. XAResource interface provides methods to associate and disassociate a transaction from the resource, such as start(), and end(), two-phase commits commit(), rollback(), and forget(), and to find active transactions before a crash recover(). |