Documentation Home
MySQL 8.0 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 43.3Mb
PDF (A4) - 43.4Mb
Man Pages (TGZ) - 297.3Kb
Man Pages (Zip) - 402.5Kb
Info (Gzip) - 4.3Mb
Info (Zip) - 4.3Mb
Excerpts from this Manual

15.3.8.1 XA Transaction SQL Statements

To perform XA transactions in MySQL, use the following statements:

XA {START|BEGIN}xid [JOIN|RESUME]XA ENDxid [SUSPEND [FOR MIGRATE]]XA PREPARExidXA COMMITxid [ONE PHASE]XA ROLLBACKxidXA RECOVER [CONVERT XID]

ForXA START, theJOIN andRESUME clauses are recognized but have no effect.

ForXA END theSUSPEND [FOR MIGRATE] clause is recognized but has no effect.

Each XA statement begins with theXA keyword, and most of them require anxid value. Anxid is an XA transaction identifier. It indicates which transaction the statement applies to.xid values are supplied by the client, or generated by the MySQL server. Anxid value has from one to three parts:

xid:gtrid [,bqual [,formatID ]]

gtrid is a global transaction identifier,bqual is a branch qualifier, andformatID is a number that identifies the format used by thegtrid andbqual values. As indicated by the syntax,bqual andformatID are optional. The defaultbqual value is'' if not given. The defaultformatID value is 1 if not given.

gtrid andbqual must be string literals, each up to 64 bytes (not characters) long.gtrid andbqual can be specified in several ways. You can use a quoted string ('ab'), hex string (X'6162',0x6162), or bit value (b'nnnn').

formatID is an unsigned integer.

Thegtrid andbqual values are interpreted in bytes by the MySQL server's underlying XA support routines. However, while an SQL statement containing an XA statement is being parsed, the server works with some specific character set. To be safe, writegtrid andbqual as hex strings.

xid values typically are generated by the Transaction Manager. Values generated by one TM must be different from values generated by other TMs. A given TM must be able to recognize its ownxid values in a list of values returned by theXA RECOVER statement.

XA STARTxid starts an XA transaction with the givenxid value. Each XA transaction must have a uniquexid value, so the value must not currently be used by another XA transaction. Uniqueness is assessed using thegtrid andbqual values. All following XA statements for the XA transaction must be specified using the samexid value as that given in theXA START statement. If you use any of those statements but specify anxid value that does not correspond to some existing XA transaction, an error occurs.

Beginning with MySQL 8.0.31,XA START,XA BEGIN,XA END,XA COMMIT, andXA ROLLBACK statements are not filtered by the default database when the server is running with--replicate-do-db or--replicate-ignore-db.

One or more XA transactions can be part of the same global transaction. All XA transactions within a given global transaction must use the samegtrid value in thexid value. For this reason,gtrid values must be globally unique so that there is no ambiguity about which global transaction a given XA transaction is part of. Thebqual part of thexid value must be different for each XA transaction within a global transaction. (The requirement thatbqual values be different is a limitation of the current MySQL XA implementation. It is not part of the XA specification.)

TheXA RECOVER statement returns information for those XA transactions on the MySQL server that are in thePREPARED state. (SeeSection 15.3.8.2, “XA Transaction States”.) The output includes a row for each such XA transaction on the server, regardless of which client started it.

XA RECOVER requires theXA_RECOVER_ADMIN privilege. This privilege requirement prevents users from discovering the XID values for outstanding prepared XA transactions other than their own. It does not affect normal commit or rollback of an XA transaction because the user who started it knows its XID.

XA RECOVER output rows look like this (for an examplexid value consisting of the parts'abc','def', and7):

mysql> XA RECOVER;+----------+--------------+--------------+--------+| formatID | gtrid_length | bqual_length | data   |+----------+--------------+--------------+--------+|        7 |            3 |            3 | abcdef |+----------+--------------+--------------+--------+

The output columns have the following meanings:

  • formatID is theformatID part of the transactionxid

  • gtrid_length is the length in bytes of thegtrid part of thexid

  • bqual_length is the length in bytes of thebqual part of thexid

  • data is the concatenation of thegtrid andbqual parts of thexid

XID values may contain nonprintable characters.XA RECOVER permits an optionalCONVERT XID clause so that clients can request XID values in hexadecimal.