SyntaxStudy
Sign Up
MySQL XA Distributed Transactions
MySQL Advanced 5 min read

XA Distributed Transactions

XA Transactions

XA transactions coordinate commits across multiple databases or resource managers (two-phase commit). Use sparingly — they are slow and complex.

Example
-- Phase 1: Prepare
XA START "txn-001";
UPDATE db1.accounts SET balance = balance - 100 WHERE id = 1;
XA END "txn-001";
XA PREPARE "txn-001";
-- Phase 2: Commit (or Rollback)
XA COMMIT "txn-001";
-- XA ROLLBACK "txn-001";
-- Check pending XA transactions:
XA RECOVER;
Pro Tip

Prefer eventual consistency via message queues (Kafka, SQS) over XA in distributed microservices.