In TDSQL for MySQL, 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
.
begin; # Start a transaction
... # Cross-set INSERT, DELETE, UPDATE, SELECT, and other non-DDL operations
commit; # Commit the transaction
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
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.
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:
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?