apache/seatunnel · error

begin next transaction failed, rollback prepared transaction

Error message

begin next transaction failed, rollback prepared transaction, xid={}

What it means

Logged by JdbcExactlyOnceSinkWriter.rollbackPrepareXidOrThrow: a new XA transaction could not be started (beginTxException) while a previously prepared transaction existed, so the writer tries to roll back the prepared xid before failing. If this rollback also throws, a JdbcConnectorException (XA_OPERATION_FAILED) chain is built combining beginTx failure and rollback failure and subsequently thrown.

Source

Thrown at seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/sink/JdbcExactlyOnceSinkWriter.java:279

            prepareXid = null;
        }
    }

    private void rollbackPrepareXidOrThrow(Exception beginTxException) {
        if (prepareXid == null) {
            return;
        }
        Xid xid = prepareXid;
        if (!xaFacade.isOpen()) {
            throw new JdbcConnectorException(
                    JdbcConnectorErrorCode.XA_OPERATION_FAILED,
                    String.format(
                            "unable to rollback prepared transaction because xaFacade is closed, xid=%s",
                            xid),
                    beginTxException);
        }
        try {
            LOG.warn("begin next transaction failed, rollback prepared transaction, xid={}", xid);
            xaFacade.rollback(xid);
            prepareXid = null;
        } catch (Exception rollbackException) {
            JdbcConnectorException rollbackFailure =
                    new JdbcConnectorException(
                            JdbcConnectorErrorCode.XA_OPERATION_FAILED,
                            String.format(
                                    "failed to rollback prepared transaction after begin next transaction failure, xid=%s",
                                    xid),
                            rollbackException);
            rollbackFailure.addSuppressed(beginTxException);
            tryRecoverPreparedTransactionsAfterRollbackFailure(xid, rollbackFailure);
            throw rollbackFailure;
        }
    }

    private void tryRecoverPreparedTransactionsAfterRollbackFailure(
            Xid failedRollbackXid, JdbcConnectorException rollbackFailure) {

View on GitHub (pinned to cf67b549a7)

Solutions

  1. Check the DB availability and network from the SeaTunnel worker node first — the root cause is in beginTxException
  2. Review the chained JdbcConnectorException (beginTxException + rollbackFailure + suppressed recovery failure) for the original XA error code
  3. Let the sink's recovery path run: after restart, recoverAndRollback resolves the in-doubt prepared transaction; ensure DB account has recovery privileges
  4. Verify XA support is correctly configured for the dialect (driver, URL, xaDataSource class) and credentials still have XA permissions
  5. If caused by a dead pooled connection, align pool keepalive/validation settings as with stale-connection issues
Defensive patterns

Strategy: retry

Validate before calling

// before relying on exactly-once, verify XA begin/prepare works and privileges exist
try (XAConnection xa = xaDs.getXAConnection(); XAResource r = xa.getXAResource()) {
    Xid probe = new XidImpl();
    r.start(probe, XAResource.TMNOFLAGS);
    r.end(probe, XAResource.TMSUCCESS);
    r.rollback(probe); // succeeds only if XA is usable
}

Try / catch

try {
    prepareCommit();
} catch (JdbcConnectorException e) {
    // error code XA_OPERATION_FAILED chains beginTx + rollback failures
    if (e.getErrorCode() == JdbcConnectorErrorCode.XA_OPERATION_FAILED) {
        LOG.error("XA begin/rollback failed; restart job to trigger recovery", e);
    }
}

Prevention

When it happens

Trigger: prepareCommit attempts to begin the next XA transaction, the begin fails (connection lost, DB down, XAER_RMERR), and then xaFacade.rollback(prepared xid) on the old prepared transaction also throws — e.g. same broken connection, facade closed, or xid unknown to the resource manager.

Common situations: Database outage or failover in the middle of a checkpoint's prepareCommit; network partition; connection pool returned a dead connection to the XA facade; exceeding DB max prepared transactions; user lacks XA privileges after a grants change.

Related errors


AI-assisted analysis of apache/seatunnel@cf67b549a7 (2026-09-10). Data as JSON: /api/errors/5a52a1a07803601b. Report an issue: GitHub.