OceanBase
English日本語简体中文
Docs Home
Products
Integrations
Release Notes

Databases

OceanBase DatabaseOceanBase Cloud OceanBase Best Practices

Tools

OceanBase Cloud PlatformOceanBase Migration ServiceOceanBase Developer CenterOceanBase Migration AssessmentOceanBase Admin ToolOceanBase Loader and DumperOceanBase DeployerKubernetes operator for OceanBaseOceanBase Diagnostic Tool OceanBase Binlog Service

Connectors and Middleware

OceanBase Database Proxy OceanBase Connector/C OceanBase Connector/J OceanBase Connector/ODBC OceanBase Connector/NETOceanBase Command-Line Client

Databases

OceanBase DatabaseOceanBase Cloud OceanBase Best Practices

Connectors and Middleware

OceanBase Database Proxy OceanBase Connector/C OceanBase Connector/J OceanBase Connector/ODBC OceanBase Connector/NETOceanBase Command-Line Client

Tools

OceanBase Cloud PlatformOceanBase Migration ServiceOceanBase Developer CenterOceanBase Migration AssessmentOceanBase Admin ToolOceanBase Loader and DumperOceanBase DeployerKubernetes operator for OceanBaseOceanBase Diagnostic Tool OceanBase Binlog Service
OceanBase DatabaseOceanBase Cloud ToolsConnectors and MiddlewareLifecycle Support Policy

OceanBase Connector/J

V2.4.5
  • What is OceanBase Connector J?
    • Overview of OceanBase Connector/J
    • Compatibility requirements
  • Install Driver
    • Install and load the OceanBase Connector/J driver
  • Instructions
    • Quick start
      • Import packages
      • Create a database connection
      • Create a Statement object
      • Run queries and retrieve ResultSet objects
      • Process ResultSet objects
      • Close ResultSet and Statement objects
      • Manage tables and data
      • Commit changes
      • Close a database connection
      • Examples
    • Data sources and URL
      • Data source overview
      • Database URL
    • Java data streams
      • Overview of Java data streams
      • LONG and LONG RAW data types
      • Use LOBs
        • Overview of LOB data types
        • LOB data interface
        • LOB locator interface
      • CHAR, VARCHAR, and RAW data types
    • Statement caching
      • Overview of statement caching
      • Use statement caching
      • Reuse statement objects
    • Call a stored procedure
    • Handle SQL exceptions
    • Result set
      • Overview of result sets
      • Limitations
      • FetchSize
      • refreshRow
      • useCursorFetch
    • Batch processing
    • Failover and Load-Balancing
      • Failover and load balancing modes
      • LoadBalance strategies
      • Load balancing strategy configuration methods
    • Rich client
    • Logging
    • Network overheads
    • Show Trace
    • Security features
    • Troubleshooting
  • Reference
    • Features specific to the Oracle mode
      • Binary Protocol (Prepared Statements)
        • COM_STMT_PREPARE_EXECUTE
        • COM_STMT_SEND_PIECE_DATA
      • Call PL stored procedures
      • Scrollability of a result set
      • Use ARRAY and STRUCT classes
      • Distributed transactions
        • Overview
        • XA components
        • Distributed transaction examples
      • Obtain comments
      • Data types supported in Oracle mode
      • Error messages in Oracle mode
    • Supported SQL and PL data types
    • Common APIs
      • Overview
      • java.sql.Connection
      • java.sql.CallableStatement
      • java.sql.DatabaseMetaData
      • java.sql.Driver
      • java.sql.PreparedStatement
      • java.sql.ParameterMetaData
      • java.sql.ResultSet
      • java.sql.ResultSetMetaData
      • java.sql.Statement
      • javax.sql.ConnectionPoolDataSource
      • javax.naming.Context
      • javax.sql.PooledConnection
      • com.oceanbase.jdbc.ObPrepareStatement
  • Release Notes
    • V2.4
      • OceanBase Connector/J V2.4.5
      • OceanBase Connector/J V2.4.4
      • OceanBase Connector/J V2.4.3
      • OceanBase Connector/J V2.4.2
      • OceanBase Connector/J V2.4.1
      • OceanBase Connector/J V2.4.0
    • V2.2
      • OceanBase Connector/J V2.2.11
      • OceanBase Connector/J V2.2.10
      • OceanBase Connector/J V2.2.7
      • OceanBase Connector/J V2.2.6
      • OceanBase Connector/J V2.2.3
      • OceanBase Connector/J V2.2.0

Download PDF

Document Feedback
  1. Documentation Center
  2. OceanBase Connector/J
  3. V2.4.5

Distributed transaction examples

Last Updated:2026-09-04 08:24:26

Import Oracle XA
Oracle XA code examples

This topic describes how to use the Oracle XA feature to implement distributed transactions.

Import Oracle XA

To use the Oracle XA feature, execute the following statements to import related objects:

import oracle.jdbc.xa.OracleXid;
import oracle.jdbc.xa.OracleXAException;
import oracle.jdbc.pool.*;
import oracle.jdbc.xa.client.*;
import javax.transaction.xa.*;

The oracle.jdbc.pool package has classes for the connection pool feature, with XA-related classes as subclasses.

Alternatively, if the code is to be run inside the database and you need to access the database for SQL operations, import oracle.jdbc.xa.server instead of oracle.jdbc.xa.client. The syntax is as follows:

import oracle.jdbc.xa.server.*;

The client and server software packages provide versions of the OracleXADataSource, OracleXAConnection, and OracleXAResource classes. The abstract versions of these three classes are in the oracle.jdbc.xa package at the top level.

Oracle XA code examples

The following takes the processing of XA transactions for example:

  1. Use XA START to start XA transactions and set them to be in the ACTIVE state.

  2. For an XA transaction in the ACTIVE state, execute the SQL statements that constitute the transaction and then execute the XA END statement. The XA END statement sets the transaction to be in the IDLE state.

  3. For an XA transaction in the IDLE state, you can execute the XA PREPARE or XA COMMIT ... ONE PHASE statement.

    • The XA PREPARE statement sets the transaction to be in the PREPARED state. In this case, the XA RECOVER statement includes the value of xid of the transaction in its output, because XA RECOVER lists all XA transactions in the PREPARED state.

    • The XA COMMIT ... ONE PHASE statement prepares and commits transactions. In this case, the XA RECOVER statement does not list the values of xid because the transactions are terminated.

  4. For an XA transaction in the PREPARED state, you can execute the XA COMMIT statement to commit and terminate the transaction, or execute XA ROLLBACK to roll back and terminate the transaction.

public static void initClass() throws SQLException {
    createTable(tableName1, "c1 int,c2 int");
    createTable(tableName2, "c1 int,c2 int");
    createTable(tableName3, "c1 varchar(200)");
}

public void obOracleXAOne() throws Exception {
    Assume.assumeTrue(sharedUsePrepare());
    Connection conn = setConnection();  
    conn.createStatement().execute(" insert into " + tableName1 + "  values(1,2)");
    JDBC4MysqlXAConnection mysqlXAConnection = new JDBC4MysqlXAConnection(
        (OceanBaseConnection) conn);
    String gtridStr = "gtrid_test_wgs_ob_oracle_xa_one";
    String bqualStr = "bqual_test_wgs_ob_oracle_xa_one";
    Xid xid = new MysqlXid(gtridStr.getBytes(), bqualStr.getBytes(), 123);
    try {
        mysqlXAConnection.start(xid, XAResource.TMNOFLAGS);
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        pstmt = conn.prepareStatement("select c1 from " + tableName1);
        rs = pstmt.executeQuery();

        while (rs.next()) {
            System.out.println(rs.getInt(1));
        }
        pstmt.close();
        pstmt = conn.prepareStatement("insert into " + tableName1 + " (c1, c2) values(?, ?)");
        pstmt.setInt(1, 12);
        pstmt.setInt(2, 12);
        pstmt.executeUpdate();
        mysqlXAConnection.end(xid, XAResource.TMSUCCESS);
        mysqlXAConnection.prepare(xid);
        mysqlXAConnection.commit(xid, false);
    } catch (Exception e) {
        e.printStackTrace();
        mysqlXAConnection.rollback(xid);
        throw e;
    }
}

public void obOracleXAOnePhase() throws Exception {
    Assume.assumeTrue(sharedUsePrepare());
    Connection conn = null;

    conn = setConnection();
    conn.createStatement().execute(" insert into " + tableName2 + "  values(1,2)");

    JDBC4MysqlXAConnection mysqlXAConnection = new JDBC4MysqlXAConnection(
        (OceanBaseConnection) conn);
    String gtridStr = "gtrid_test_wgs_ob_oracle_xa_one_phase";
    String bqualStr = "bqual_test_wgs_ob_oracle_xa_one_phase";

    Xid xid = new MysqlXid(gtridStr.getBytes(), bqualStr.getBytes(), 123);
    try {
        mysqlXAConnection.start(xid, XAResource.TMNOFLAGS);
        // ps test
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        pstmt = conn.prepareStatement("select c1 from " + tableName2 + "");
        rs = pstmt.executeQuery();

        while (rs.next()) {
            System.out.println(rs.getInt(1));
        }

        pstmt.close();

        pstmt = conn.prepareStatement("insert into " + tableName2 + " (c1, c2) values(?, ?)");
        pstmt.setInt(1, 12);
        pstmt.setInt(2, 12);
        pstmt.executeUpdate();

        mysqlXAConnection.end(xid, XAResource.TMSUCCESS);
        mysqlXAConnection.commit(xid, true);
    } catch (Exception e) {
        mysqlXAConnection.rollback(xid);
        throw e;
    }
}

Note

Call setConnection() to concatenate useServerPrepStmts=true to the URL in the format of jdbc:oceanbase://xx:xx/xx?useServerPrepStmts=true&....

Previous topic

XA components
Last

Next topic

Obtain comments
Next
on this page
Import Oracle XA
Oracle XA code examples