How to Fix MySQL Error 1402 (ER_XA_RBROLLBACK)

Published
Length
3 min read

Luis Felipe Dias

All posts

You see mysql error 1402 when the database engine rolls back a distributed transaction branch. To fix it, end the failed branch in your app and start a fresh transaction. MySQL logs this failure under symbol ER_XA_RBROLLBACK and SQLSTATE XA100. The message states XA_RBROLLBACK: Transaction branch was rolled back. This happens when a two-phase commit fails before the final commit step. What causes mysql error 1402 in distributed transactions Distributed transactions span multiple databases or

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You see mysql error 1402 when the database engine rolls back a distributed transaction branch. To fix it, end the failed branch in your app and start a fresh transaction.

MySQL logs this failure under symbol ER_XA_RBROLLBACK and SQLSTATE XA100. The message states XA_RBROLLBACK: Transaction branch was rolled back. This happens when a two-phase commit fails before the final commit step.

What causes mysql error 1402 in distributed transactions

Distributed transactions span multiple databases or services at the same time. A transaction manager coordinates these branches so they all commit or fail together. When one local branch hits a conflict, the database aborts that specific work unit.

Row lock deadlocks are the most common cause of this rollback error. Two concurrent branches might wait on each other to release the same database rows. MySQL spots the cycle and kills one branch to free the blocked session.

Lock wait timeouts also trigger ER_XA_RBROLLBACK during peak traffic hours. If a branch waits too long for a row lock, the engine cancels it. Network drops between the transaction manager and the server cause similar failures.

When a session drops mid-transaction, MySQL cleans up the active branch state. If the app reconnects and tries to prepare that branch, the server returns code 1402. Cluster node failovers also break open branches and force an automatic rollback.

How to diagnose and fix ER_XA_RBROLLBACK safely

Start your check by reading the database deadlock log and active lock tables. Look for slow queries or bulk updates that hold row locks for too long. Match the timestamp of the error in your app log to the database log.

Never try to reuse the same transaction ID after receiving SQLSTATE XA100. The database still holds the aborted state until the transaction manager acknowledges the failure. Your app must close or end the failed branch before starting a new attempt.

MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Once the old branch closes, your app can retry the business work with a new ID. Add a short pause with random jitter before the retry runs in production. This simple step stops multiple app workers from colliding on the same rows again.

You can also shrink your transaction size to prevent future rollbacks. Keep remote API calls and slow file reads outside the distributed transaction boundary. Open the XA branch, run the needed queries, and prepare the branch right away.

Check the order in which your app updates tables across all branches. If every service updates rows in the same primary key order, deadlocks drop fast. Avoid granting superuser roles to app workers just to bypass lock limits.

Preventing orphaned XA branches in production

Sometimes an app crashes right after a branch prepares or rolls back. This leaves dangling transactions inside the database engine that hold onto row locks. These stuck branches block fresh traffic and cause a wave of new 1402 errors.

Database teams should check for lingering prepared branches during routine maintenance windows. When you find a dead branch from a crashed worker, roll it back cleanly. Keep your connection pools tuned so idle sessions do not drop during active work.

When engineers inspect stuck transactions in production, they still need strict data safety controls. Granting raw admin credentials to debug lock tables creates major security risks for the team. Instead, teams can route debug queries through a layer that rewrites emails and card numbers in results.

Check MySQL queries and results on the wire

MySQL grants control who logs in. They do not check single queries or mask sensitive columns once a session starts. With hoop.dev, you run an open-source proxy, a sidecar for your MySQL, inline in the MySQL wire protocol. On every query an AI agent or human runs against production MySQL, RDS, or Aurora, hoop.dev decides to pass, mask, block, or hold it for review.

Multi-statement batches and prepared queries are split and checked one by one by hoop.dev guardrails, which refuse banned queries with a native MySQL error or hold risky ones for approval. On the response path, hoop.dev data masking rewrites string values in returned rows in memory, and every decision is logged. See the MIT-licensed project on GitHub.

Frequently asked questions

What does SQLSTATE XA100 mean in MySQL?

SQLSTATE XA100 means the database rolled back an XA transaction branch due to an internal error. It pairs with error code 1402 and symbol ER_XA_RBROLLBACK when a deadlock or timeout occurs.

Can I commit an XA branch after getting mysql error 1402?

No, you cannot commit a branch once the server returns error 1402. The database has already undone the changes, so your app must end the branch and retry with a new transaction ID.

How do I stop deadlocks from triggering ER_XA_RBROLLBACK?

Keep distributed transactions short and update tables in a consistent row order across all services. You should also remove slow network calls from inside the transaction block to release locks faster.

The query runs. The emails stay hidden.