Home      Discussion      Topics      Dictionary      Almanac
Signup       Login
Java Transaction API

Java Transaction API

Discussion
Ask a question about 'Java Transaction API'
Start a new discussion about 'Java Transaction API'
Answer questions from other users
Full Discussion Forum
 
Encyclopedia
The Java Transaction API (JTA) is one of the Java Enterprise Edition (Java EE) API
Application programming interface
An application programming interface is a source code based specification intended to be used as an interface by software components to communicate with each other...

s allowing distributed transactions to be done across multiple XA
X/Open XA
In computing, the XA standard is a specification by The Open Group for distributed transaction processing . It describes the interface between the global transaction manager and the local resource manager...

 resources in a Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 environment. JTA is a specification developed under the Java Community Process
Java Community Process
The Java Community Process or JCP, established in 1998, is a formalized process that allows interested parties to get involved in the definition of future versions and features of the Java platform....

 as JSR 907. JTA provides for:
  • demarcation of transaction boundaries
  • X/Open XA API allowing resources to participate in transactions.

X/Open XA architecture


In the X/Open XA architecture, a transaction manager or transaction processing monitor (TP monitor), coordinates the transactions across multiple resources such as databases and message queues. Each resource has its own manager. The resource manager typically has its own API for manipulating the resource, for example the JDBC
Java Database Connectivity
Java DataBase Connectivity, commonly referred to as JDBC, is an API for the Java programming language that defines how a client may access a database. It provides methods for querying and updating data in a database. JDBC is oriented towards relational databases...

 API used by relational databases. In addition, the resource manager allows a TP monitor to coordinate a distributed transaction between its own and other resource managers. Finally, there is the application which communicates with the TP monitor to begin, commit or rollback the transactions. The application also communicates with the individual resources using their own API to modify the resource.

JTA implementation of the X/Open XA architecture


The JTA API consists of classes in two Java package
Java package
A Java package is a mechanism for organizing Java classes into namespaces similar to the modules of Modula. Java packages can be stored in compressed files called JAR files, allowing classes to download faster as a group rather than one at a time...

s:

The JTA is modelled on the X/Open XA architecture, but it defines two different APIs for demarcating transaction boundaries. It distinguishes between an application server such as an EJB
Enterprise JavaBean
Enterprise JavaBeans is a managed, server-side component architecture for modular construction of enterprise applications.The EJB specification is one of several Java APIs in the Java EE specification. EJB is a server-side model that encapsulates the business logic of an application...

 server and an application component. It provides an interface, , that is used by the application server itself to begin, commit and rollback the transactions. It provides a different interface, the , that is used by general client code such as a servlet or an EJB to manage the transactions.

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
    Java Message Service
    The Java Message Service API is a Java Message Oriented Middleware API for sending messages between two or more clients. JMS is a part of the Java Platform, Enterprise Edition, and is defined by a specification developed under the Java Community Process as JSR 914...

  • generalized EIS (Enterprise Information System) resources use Java EE connector API.

Java Transaction API


The Java Transaction API consists of three elements: a high-level application
transaction demarcation interface, a high-level transaction manager interface intended
for an application server, and a standard Java mapping of the X/Open XA protocol
intended for a transactional resource manager.

UserTransaction interface


The javax.transaction.UserTransaction interface provides the application the
ability to control transaction boundaries programmatically. This interface may be used
by Java client programs or EJB beans.

The UserTransaction.begin 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
Java transaction service
The Java Transaction Service is a specification for building a transaction manager that maps onto the Object Management Group Object Transaction Service used in the Common Object Request Broker Architecture architecture...

 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.

UserTransaction support in EJB server


EJB servers are required to support the UserTransaction interface for use by EJB
beans with the BEAN value in the @TransactionManagement annotation (this is called bean-managed transactions or BMT). The UserTransaction
interface is exposed to EJB components through either the EJBContext interface using the
getUserTransaction method, or directly via injection using the general @Resource annotation. Thus, an EJB application does not interface with the
Transaction 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
Enterprise JavaBeans Specification. (The underlying interaction between the EJB
Server and the TM is transparent to the application; the burden of implementing transaction management is on the EJB container and server provider.)

The code sample below illustrates the usage of UserTransaction via bean-managed transactions in an EJB session bean:


@Stateless
@TransactionManagement(BEAN)
public class ExampleBean {

@Resource
private UserTransaction utx;

public void foo {
// start a transaction
utx.begin;

// Do work

// Commit it
utx.commit;
}
}


Alternatively, the UserTransaction can be obtained from the SessionContext:


@Stateless
@TransactionManagement(BEAN)
public class ExampleBean {

@Resource
private SessionContext ctx;

public void foo {
UserTransaction utx = ctx.getUserTransaction;

// start a transaction
utx.begin;

// Do work

// Commit it
utx.commit;
}
}


Note though that in the example above if the @TransactionManagement(BEAN) 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 UserTransaction is thus not necessary in EJB programming, but might be needed for very specialized code.

UserTransaction support in JNDI


The UserTransaction should be available under java:comp/UserTransaction (if a JTA implementation is installed in the environment).

UserTransaction support in Java SE


You do not necessarily need a Java EE application server to have JTA or UserTransaction functionality. Like many other parts of the Java EE API, JTA tons based on a Servlet container like Tomcat
Apache Tomcat
Apache Tomcat is an open source web server and servlet container developed by the Apache Software Foundation...

 or Jetty
Jetty (web server)
Jetty is a pure Java-based HTTP client/server, WebSocket client/server and servlet container developed as a free and open source project as part of the Eclipse Foundation...

. However, although the power and reliability of JTA can be made available in such environments, the transparency and ease of use of declarative transactions remains an asset of application servers. Alternatively, a non-Java EE application server can be built on top of a container like Spring, which can give a very similar paradigm for developing reliable Java applications, just with a different API.

Opensource JTA implementations


There exist a number of active (as of September 2010) opensource JTA implementations.

JBossTS


JBossTS, formerly known as Arjuna Transaction Service, comes with a very robust implementation, which supports both the JTA and JTS APIs. JBossTS comes with a recovery service, which could be run as a separate process from your application processes. JBossTS is the default transaction manager for JBoss AS. It does not support out-of-the box integration with the Spring framework, but it is easy to integrate.

Atomikos TransactionsEssentials


Atomikos's JTA implementation's documentation and literature on the internet show that it is a production quality implementation, which also supports recovery and some exotic features beyond the JTA API. Atomikos provides out-of-the-box Spring integration along with some nice examples. It also provides support for pooled connections for both database and JMS resources.

Bitronix JTA


Bitronix claims to support transaction recovery as good as or even better than some of the commercial products. Bitronix also provides connection pooling and session pooling out of the box.

External links