Java Reference
In-Depth Information
Java Transaction API 1.2
The Java Transaction API 1.2 Specification was developed under JSR 907. This sec-
tion just gives you an overview of improvement in the API. The complete document
specification (for more information) can be downloaded from
http://jcp.org/aboutJava/
The Java Transaction API
The
Java Transaction API
(
JTA
) is a standard Java API for managing transactions
on one or more resources (distributed transactions) in server environments. It consist
of three main APIs:
javax.transaction.UserTransaction
interface used by
applications for explicit transaction demarcation,
javax.transaction.TransactionManager
interface used by application serv-
ers to demarcate transactions implicitly on behalf of the application, and
javax.transaction.xa.XAResource
, which is a Java mapping of the standard
XA interface for distributed transaction processing.
JTA in action
As we said, JTA transactions are used in Java EE environments. In order to enable
this transaction type, the
transaction-type
attribute of the persistence unit should
be set to
JTA
instead of
RESOURCE_LOCAL
and the data source (if there is one),
should be defined within the
<jta-datasource>
element. The following code gives
an example of a persistence unit to manage transactions using JTA:
<?xml version="1.0" encoding="UTF-8"?>
<persistence
version="2.1"xmlns="http://xmlns.jcp.org/xml/ns/
persistence"
xmlns:xsi="http://www.w3.org/2001/
XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/
ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/
persistence_2_1.xsd">