JTA Transaction Model Specification
In this article, we will delve into the Java Transaction API (JTA) specification and its role in providing distributed transaction services for the J2EE platform. We will explore the JTA transaction model, its components, and the interfaces defined in the specification.
JTA Transaction Model
The JTA transaction model provides a standardized way of defining distributed transactions in a Java environment. It is based on the DTP (Distributed Transaction Processing) model, which consists of five constituent elements:
- Application: The application that requires a distributed transaction.
- Resource Manager (RM): The software that stores data, such as a relational database or messaging middleware.
- Transaction Manager ™: The component that coordinates the distributed transaction.
- Communication Resource Manager (CRM): The component that enables communication between the application and the resource manager.
- Communication Protocol: The protocol used for communication between the application and the resource manager.
In the JTA specification, an additional element is introduced: the Application Server. The application server provides the transaction manager functions, such as transaction demarcation, transactional resource management, synchronization, and transaction context propagation.
Components of the JTA Transaction Model
The JTA transaction model consists of the following components:
- Transaction Manager ™: The central component that coordinates the distributed transaction. It provides transaction demarcation, transactional resource management, synchronization, and transaction context propagation.
- Application Server: The container that runs the application. It provides the transaction manager functions.
- Application: The application that requires a distributed transaction.
- Resource Manager (RM): The software that stores data, such as a relational database or messaging middleware.
- Communication Resource Manager (CRM): The component that enables communication between the application and the resource manager.
Interfaces Defined in the JTA Specification
The JTA specification defines several interfaces that must be implemented by the resource manager and the transaction manager:
- javax.transaction.Status: An interface that defines the state of the transaction.
- javax.transaction.Synchronization: An interface that defines the synchronization mechanism.
- javax.transaction.Transaction: An interface that defines the transaction.
- javax.transaction.TransactionManager: An interface that defines the transaction manager.
- javax.transaction.UserTransaction: An interface that defines the user transaction.
- javax.transaction.TransactionSynchronizationRegistry: An interface that defines the transaction synchronization registry.
- javax.transaction.xa.XAResource: An interface that defines the resource manager’s interface to the transaction manager.
- javax.transaction.xa.Xid: An interface that defines the transaction identifier.
Implementing the JTA Interfaces
The JTA specification requires that the resource manager and the transaction manager implement the interfaces defined in the specification. The resource manager must implement the XAResource interface, which defines the methods that will be invoked by the transaction manager. The transaction manager must implement the UserTransaction interface, which defines the user transaction.
Building a Distributed Transaction
To build a distributed transaction, the application must use the UserTransaction interface provided by the transaction manager. The application must also use the XAResource interface provided by the resource manager to access the data.
Here is an example of building a distributed transaction:
UserTransaction userTransaction = ...;
try {
userTransaction.begin();
// execute the transaction branch 1
conn1 = db1.getConnection();
ps1 = conn1.prepareStatement("INSERT into user (name, age) VALUES ('tianshouzhi', 23)");
ps1.executeUpdate();
// execute the transaction branch 2
conn2 = db2.getConnection();
ps2 = conn2.prepareStatement("INSERT into user (name, age) VALUES ('tianshouzhi', 23)");
ps2.executeUpdate();
// submission, two-phase commit occurs inside this method
userTransaction.commit();
} catch (Exception e) {
try {
userTransaction.rollback();
} catch (SystemException ignore) {
}
}
Note that, in a distributed transaction, when we need to commit or roll back a transaction, we should no longer use the commit and rollback methods provided by the Connection interface. Instead, we should use the commit and rollback methods provided by the UserTransaction interface.