Distributed Transaction

Last updated: 2020-10-30 11:02:50

    In TencentDB for TDSQL, a transaction usually involves data of multiple physical nodes. Such transactions are called distributed transactions.
    TDSQL supports XA and non-XA distributed transaction protocols. By default, TDSQL (kernel version 5.7 or later) supports distributed transactions which are imperceptible to the client and as easy-to-use as non-distributed transactions.
    Distributed transactions in TDSQL adopt a two-phase commit protocol (2PC) to ensure the atomicity and consistency of transactions, and support such isolation levels as Read committed, Repeatable read, and Serializable.

    Non-XA Distributed Transactions

    begin;     # Start a transaction
    ...    # Cross-set INSERT, DELETE, UPDATE, SELECT, and other non-DDL operations
    commit;    # Commit the transaction

    XA Distributed Transactions

    XA distributed transactions refer to cross-instance transactions.

    xa begin ''; # Start an XA transaction. The transaction identifier is generated by the system, so you can pass in an empty string.
    ...    # Cross-set INSERT, DELETE, UPDATE, SELECT, and other non-DDL operations
    select gtid();   # Get the XA transaction identifier, which is assumed as 'xid' in the following statements
    xa prepare 'xid';    # Prepare the transaction
    xa commit/rollback 'xid'; # Commit or roll back the transaction

    New Transactions APIs

    • select gtid(): this API is used to get the globally unique identifier of the distributed transaction. If no value is returned, the transaction is not a distributed transaction.

      • The format of non-XA distributed transaction identifier is: 'Gateway ID' - 'Proxy random value' - 'Serial number' - 'Timestamp' - 'Partition number', such as c46535fe-b6-dd-595db6b8-25.
      • The format of XA distributed transaction identifier is: 'ex' - 'Gateway ID' - 'Proxy random value' - 'Serial number' - 'Timestamp' - 'Partition number', such as ex-c46535fe-b6-dd-595db6b8-25.
    • select gtid_state("Globally unique identifier of the distributed transaction"): this API is used to get the transaction status (in 3 seconds by default) after an exception occurs in committing the transaction. The following status may be returned:

      • COMMIT: indicates that the transaction has been or is going to be committed.
      • ABORT: indicates that the transaction is going to be rolled back.
      • Empty value: the transaction status will be cleared after an hour, so there are two possibilities:
        • For a query after an hour, it indicates that the transaction status has been cleared.
        • For a query within an hour, it indicates that the transaction is going to be rolled back.
    • xa boost 'Globally unique identifier of the distributed transaction': after an exception occurs in committing a non-XA transaction, the transaction will be automatically committed or rolled back by the backend component in a certain period of time (30 seconds by default). If you are unwilling to wait for such a long time, you can repeatedly call this API to make the system commit or roll back the transaction in a timely manner. This API will return the status of the transaction (i.e., committed or rolled-back).

    • xa lockwait: this API is used to show the wait-for relationship between the distributed transactions. You can convert it to a graph using the dot tool.

    • xa show: this API is used to show transactions that are active on the proxy.

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help