Backend Development 14 min read

Understanding X/Open DTP Model, XA Specification, and Two-Phase Commit with Atomikos

This article explains the X/Open Distributed Transaction Processing model, the XA specification, the two‑phase commit protocol, and demonstrates how to configure and use Atomikos in Java for managing distributed transactions across multiple resources.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Understanding X/Open DTP Model, XA Specification, and Two-Phase Commit with Atomikos

X/Open DTP Model and XA Specification

X/Open, now known as the Open Group, is an independent organization that defines industry technical standards. Its website is www.opengroup.org. The group is supported by major companies and participates in standard creation. The following diagram shows the current main members of the Open Group.

For Distributed Transaction Processing (DTP), X/Open provides the following reference documents:

DTP Reference Model:

DTP XA Specification:

DTP Model Elements

The third edition of "Distributed Transaction Processing: Reference Model" defines five basic elements:

Application Program (AP) : defines transaction boundaries and operates on resources within those boundaries.

Resource Manager (RM) : such as databases or file systems, provides access to resources.

Transaction Manager (TM) : assigns a unique transaction ID, monitors progress, and handles commit/rollback.

Communication Resource Manager (CRM) : controls communication between distributed applications within or across TM domains.

Communication Protocol (CP) : provides the underlying communication service for CRM.

A DTP model instance must contain at least three components: AP, one or more RMs, and TM. The diagram below illustrates a typical instance.

This diagram is similar to the cross‑database transaction concept: a single application (AP) operates on multiple resource managers (RMs). The AP declares a global transaction via the TM, performs operations on the RMs, and finally instructs the TM to commit or roll back the global transaction.

XA Specification

In a local DTP model instance, only AP, RMs, and TM are needed. Their interactions are shown in the following diagram.

The subject of this X/Open specification is interface (3) in the diagram above, the XA interface by which TMs and RMs interact. For more details on this model and diagram, including detailed definitions of each component, see the referenced DTP guide.

The XA specification primarily defines the RM‑TM interaction interface. The following diagram highlights where XA fits within the DTP model.

Two‑Phase Commit (2PC)

Although the two‑phase commit protocol is not introduced by the XA specification, XA optimizes it. The protocol consists of two phases:

Phase 1 – Prepare : The TM asks each RM to prepare its transaction branch. If an RM can commit, it persists its work and replies positively; otherwise it replies negatively and rolls back its branch.

For example, in MySQL the TM sends a "prepare" request; the database records the changes and marks the transaction as "ready to commit" before responding.

Phase 2 – Commit/Rollback : Based on the prepare responses, the TM decides to commit or roll back. If all RMs prepared successfully, the TM sends a commit request; otherwise it sends a rollback request.

In MySQL, a successful prepare leads to a "commit" request that finalizes the transaction; any failure triggers a rollback across all involved databases.

Problems with 2PC

Synchronous blocking : All participants block while waiting for others, preventing other work.

Single point of failure : If the TM crashes, participants remain blocked, especially during the second phase.

Data inconsistency : Network issues or TM failure after some participants have committed can leave the system in an inconsistent state.

Java Transaction API (JTA) and Java Transaction Service (JTS)

JTA and JTS provide distributed transaction services for the J2EE platform. Distributed transactions consist of a Transaction Manager and one or more XA‑capable Resource Managers. JTA abstracts the underlying resources, allowing applications to participate in transactions transparently, though XA incurs higher overhead.

Atomikos

Atomikos is an open‑source transaction manager for Java. It offers two products:

TransactionEssentials – a free, open‑source version.

ExtremeTransactions – a commercial edition.

TransactionEssentials implements the JTA/XA interfaces, providing classes such as

com.atomikos.icatch.jta.UserTransactionImp

,

com.atomikos.icatch.jta.UserTransactionManager

, and

com.atomikos.icatch.jta.TransactionImp

. It also wraps XA‑capable JDBC pools and JMS clients. The XADataSource is encapsulated by

AtomikosDataSourceBean

.

Atomikos is added to a project as a JAR; no separate transaction manager process is required (unlike Seata).

Example code:

<code>public class AtomikosDemo {

    public static AtomikosDataSourceBean createDs1() {
        AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
        ds.setUniqueResourceName("tksResource");
        ds.setXaDataSourceClassName("com.mysql.cj.jdbc.MysqlXADataSource");
        Properties xaProperties = new Properties();
        xaProperties.setProperty("url", "jdbc:mysql://localhost:3306/tks?useSSL=false&serverTimezone=UTC");
        xaProperties.setProperty("user", "root");
        xaProperties.setProperty("password", "123123");
        ds.setXaProperties(xaProperties);
        ds.setMinPoolSize(10);
        ds.setMaxPoolSize(10);
        ds.setBorrowConnectionTimeout(30);
        ds.setMaxLifetime(60);
        ds.setMaintenanceInterval(60);
        return ds;
    }

    public static AtomikosDataSourceBean createDs2() {
        AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
        ds.setUniqueResourceName("testjapResource");
        ds.setXaDataSourceClassName("com.mysql.cj.jdbc.MysqlXADataSource");
        Properties xaProperties = new Properties();
        xaProperties.setProperty("url", "jdbc:mysql://localhost:3306/testjpa?useSSL=false&serverTimezone=UTC");
        xaProperties.setProperty("user", "root");
        xaProperties.setProperty("password", "123123");
        ds.setXaProperties(xaProperties);
        ds.setMinPoolSize(10);
        ds.setMaxPoolSize(10);
        ds.setBorrowConnectionTimeout(30);
        ds.setMaxLifetime(60);
        ds.setMaintenanceInterval(60);
        return ds;
    }

    public static void main(String[] args) throws Exception {
        AtomikosDataSourceBean ds1 = createDs1();
        AtomikosDataSourceBean ds2 = createDs2();
        Connection conTks = null;
        Connection conTestJpa = null;
        PreparedStatement psTks = null;
        PreparedStatement psTestJpa = null;
        UserTransaction tx = new UserTransactionImp();
        try {
            tx.begin();
            conTks = ds1.getConnection();
            psTks = conTks.prepareStatement("update users set score = 77777 where id = ?");
            psTks.setInt(1, 1);
            int tksRes = psTks.executeUpdate();
            System.out.println("tks execution result: " + tksRes);
            conTestJpa = ds2.getConnection();
            psTestJpa = conTestJpa.prepareStatement("update users set password = '888888' where id = ?");
            psTestJpa.setInt(1, 2);
            int testJpaRes = psTestJpa.executeUpdate();
            System.out.println("testJpa execution result: " + testJpaRes);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
        } finally {
            if (psTks != null) psTks.close();
            if (psTestJpa != null) psTestJpa.close();
        }
    }
}
</code>

In a Spring Boot project, add the following Maven dependency:

<code>&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-jta-atomikos&lt;/artifactId&gt;
&lt;/dependency&gt;
</code>

Configure the data source properties accordingly. The article ends here.

Javabackend developmentdistributed transactionTwo-Phase CommitAtomikosXA Specification
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.