The Jakarta Transactions (JTA; formerly Java Transaction API), one of the
Jakarta EE
Jakarta EE, formerly Java Platform, Enterprise Edition (Java EE) and Java 2 Platform, Enterprise Edition (J2EE), is a set of specifications, extending Java SE with specifications for enterprise features such as distributed computing and web se ...
API
An application programming interface (API) is a way for two or more computer programs to communicate with each other. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how ...
s, enables
distributed transaction A distributed transaction is a database transaction in which two or more network hosts are involved. Usually, hosts provide transactional resources, while the transaction manager is responsible for creating and managing a global transaction that enc ...
s to be done across multiple
X/Open XA
For transaction processing in computing, the X/Open XA standard (short for "eXtended Architecture") is a specification released in 1991 by X/Open (which later merged with The Open Group) for distributed transaction processing (DTP).
Goals
The ...
resources in a
Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
environment. JTA was a specification developed under the
Java Community Process
The Java Community Process (JCP), established in 1998, is a formalized mechanism that allows interested parties to develop standard technical specifications for Java technology. Anyone can become a JCP Member by filling a form available at thJCP we ...
as JSR 907. JTA provides for:
*demarcation of transaction boundaries
*
X/Open XA
For transaction processing in computing, the X/Open XA standard (short for "eXtended Architecture") is a specification released in 1991 by X/Open (which later merged with The Open Group) for distributed transaction processing (DTP).
Goals
The ...
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 resource manager. The resource manager typically has its own API for manipulating the resource, for example the
JDBC
Java Database Connectivity (JDBC) is an application programming interface (API) for the programming language Java, which defines how a client may access a database. It is a Java-based data access technology used for Java database connectivity. ...
API to work with 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
roll back
''Roll Back'' is an album by Irish rock band Horslips, their first since ''Short Stories/Tall Tales'' 25 years earlier. It is a collection of acoustic re-workings of various songs from the band's catalogue.
Background
In March 2004, three Ho ...
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 A Java package organizes Java classes into namespaces,
providing a unique namespace for each type it contains.
Classes in the same package can access each other's package-private and protected members.
In general, a package can contain the foll ...
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
Jakarta Enterprise Beans (EJB; formerly Enterprise JavaBeans) is one of several Java APIs for modular construction of enterprise software. EJB is a server-side software component that encapsulates business logic of an application. An EJB ...
server and an application component. It provides an interface, , that is used by the application server itself to begin, commit and roll back 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
* generalized EIS (
Enterprise Information System
An Enterprise Information System (EIS) is any kind of information system which improves the functions of enterprise business processes by integration. This means typically offering high quality of service, dealing with large volumes of data and c ...
) resources use
Java EE Connector API.
Application Programming Interface
The Jakarta Transactions 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 interface provides the application the
ability 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 the
ability to control transaction boundaries declaratively. This annotation can be applied to any class that the Jakarta EE specification
defines as a managed bean (which includes CDI managed beans).
The code sample below illustrates the usage of @Transactional 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
Jakarta Enterprise Beans (EJB; formerly Enterprise JavaBeans) is one of several Java APIs for modular construction of enterprise software. EJB is a server-side software component that encapsulates business logic of an application. An EJB ...
specification.
@TransactionScoped annotation
The annotation provides the application the
ability 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 @TransactionScoped 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
EJB
Jakarta Enterprise Beans (EJB; formerly Enterprise JavaBeans) is one of several Java APIs for modular construction of enterprise software. EJB is a server-side software component that encapsulates business logic of an application. An EJB ...
servers are required to support the UserTransaction interface for use by EJB
beans with the BEAN value in the 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
Jakarta Enterprise Beans 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.
JSR 220: Enterprise JavaBeans,Version 3.0, EJB 3.0 Expert Group, Sun Microsystems, 2006
/ref>)
The code sample below illustrates the usage of UserTransaction via bean-managed transactions in an EJB session bean:
@Stateless
@TransactionManagement(BEAN)
public class ExampleBean
Alternatively, the UserTransaction can be obtained from the SessionContext:
@Stateless
@TransactionManagement(BEAN)
public class ExampleBean
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).
See also
* Java transaction service The Java Transaction Service (JTS) is a specification for building a transaction manager that maps onto the Object Management Group
The Object Management Group (OMG) is a computer industry standards consortium. OMG Task Forces develop enterpris ...
References
External links
*
JSR 907
Atomikos transaction manager website
Narayana transaction manager website
Bitronix transaction manager website
{{DEFAULTSORT:Java Transaction Api
Transaction API
Transaction API
Java APIs
Articles with example Java code