PolarDB-X Distributed Transaction FAQs

Updated at:

This topic answers frequently asked questions about PolarDB-X distributed transactions.

How does PolarDB-X ensure transaction consistency when the network is disconnected during the two-phase commit of a distributed transaction? If a node transaction fails, will the entire transaction be rolled back?

Before the Commit phase, a two-phase Prepare Phase is performed on the different shards involved in the write operations:

  1. If the Prepare Phase succeeds, even if the Commit phase fails due to a network disconnection, the instance will ensure the transaction continues to commit based on the written transaction logs after recovery.

  2. If the Prepare Phase does not succeed, the distributed transaction returns a failure.

When a user invokes a distributed transaction through the SDK and the transaction is rolled back, how does the client detect the data inconsistency?

Transaction result feedback mechanism: If any error occurs during database processing, the system explicitly returns a failure status to the application. A success response is returned only when all database operations complete successfully.

  • Data consistency guarantee: When a distributed transaction returns a failure, it means that the transactions on all participating shards have not been committed (or have all been rolled back). In this state, other applications will never observe inconsistent intermediate states when reading data.

  • Partial commit scenario handling: During distributed transaction processing, there may be an intermediate state where some shard transactions have been committed while others have not. PolarDB-X 2.0 ensures through the TSO (Timestamp Oracle) mechanism that other applications can only see fully committed transaction data, strictly maintaining the ACID properties of transactions and achieving strongly consistent distributed transactions.

When a distributed transaction contains two SQL statements, if SQL1 succeeds and SQL2 fails during the Commit phase, will SQL2 also be rolled back if SQL1 is rolled back?

BEGIN; 
UPDATE account SET balance = balance - 20 WHERE name = 'Alice'; //sql 1 
UPDATE account SET balance = balance + 20 WHERE name = 'Bob'; //sql 2 
COMMIT;

The commit of a PolarDB-X distributed transaction is divided into two phases: Prepare and Commit.

  • Prepare phase: Each shard (DN) executes a transaction pre-commit, locks resources, and ensures the transaction can be executed.

  • Commit phase: The transaction coordinator node (CN) sends the final commit command to all participating nodes.

Failure handling mechanisms in different phases

  • Failure in the Commit phase (SQL1 succeeds, SQL2 fails)

    • Because the transaction has already entered the Commit phase, the Prepare phase has fully succeeded, so the transaction will eventually be committed successfully.

    • The CN continuously retries the Commit of SQL2 in the background until it succeeds.

    • Data consistency guarantee: The TSO (Timestamp Oracle) mechanism ensures that other transactions can only read consistent data (they can only see fully committed transactions). No partial visibility occurs.

  • Failure in the Prepare phase (SQL2 fails, SQL1 succeeds)

    • The entire distributed transaction fails and all changes are rolled back, including the SQL1 statement that had already succeeded.

    • No data inconsistency occurs.