Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days

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:

  • Client-level interface

  • Transaction manager interface

  • Java mapping of the X/Open XA protocol intended for a resource manager

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:

  • public interface javax.transaction.UserTransaction

  • public interface javax.transaction.TransactionManager

  • public interface javax.transaction.Transaction

  • public interface javax.transaction.Synchronization

  • public interface javax.transaction.Status

The javax.transaction.xa package includes these interfaces:

  • public interface javax.transaction.xa.XAResource

  • public interface javax.transaction.xa.Xid

The details of these interfaces are discussed in the following sections.

UserTransaction Interface

The 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:

  • begin() Associates a current thread with a newly created transaction

  • commit() Completes the transaction associated with the current thread

  • rollback() Rolls back the transaction

TransactionManager Interface

The 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:

  • getTransaction() This returns the transaction object associated with the current thread calling this function.

  • resume() This function accepts the transaction object. It resumes the transaction context associated with the calling thread with the transaction object that is passed.

  • suspend() It suspends the transaction associated with the current thread and returns the suspended transaction object.

Transaction Interface

Every 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

  • enlistResource() It enlists the resource (XAResource object) that is passed to it. It returns a Boolean value indicating whether the resource was successfully enlisted.

  • delistResource() It delists the resource (XAResource object) that is passed to it and is associated with the current transaction context. It returns a Boolean value indicating whether the resource was successfully delisted.

Synchronization Interface

The 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 Interface

This 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().

Категории