13.3.8.1 XA Transaction SQL StatementsXA事务SQL语句

To perform XA transactions in MySQL, use the following statements:要在MySQL中执行XA事务,请使用以下语句:

XA {START|BEGIN} xid [JOIN|RESUME]

XA END xid [SUSPEND [FOR MIGRATE]]

XA PREPARE xid

XA COMMIT xid [ONE PHASE]

XA ROLLBACK xid

XA RECOVER [CONVERT XID]

For XA START, the JOIN and RESUME clauses are recognized but have no effect.对于XA START,可以识别JOINRESUME子句,但不起作用。

For XA END the SUSPEND [FOR MIGRATE] clause is recognized but has no effect.对于XA END,可以识别SUSPEND [For MIGRATE]子句,但不起作用。

Each XA statement begins with the XA keyword, and most of them require an xid value. 每个XA语句都以XA关键字开头,其中大多数语句都需要xid值。An xid is an XA transaction identifier. xid是XA事务标识符。It indicates which transaction the statement applies to. 它指示该语句适用于哪个事务。xid values are supplied by the client, or generated by the MySQL server. xid值由客户端提供,或由MySQL服务器生成。An xid value has from one to three parts:xid值由一到三部分组成:

xid: gtrid [, bqual [, formatID ]]

gtrid is a global transaction identifier, bqual is a branch qualifier, and formatID is a number that identifies the format used by the gtrid and bqual values. gtrid是全局事务标识符,bqual是分支限定符,formatID是标识gtridbqual值使用的格式的数字。As indicated by the syntax, bqual and formatID are optional. 如语法所示,bqualformatID是可选的。The default bqual value is '' if not given. 如果未给定,则默认bqual值为''The default formatID value is 1 if not given.如果未给定,则默认formatID值为1。

gtrid and bqual must be string literals, each up to 64 bytes (not characters) long. gtridbqual必须是字符串文字,每个文字的长度不得超过64字节(不是字符)。gtrid and bqual can be specified in several ways. gtridbqual可以用几种方式指定。You can use a quoted string ('ab'), hex string (X'6162', 0x6162), or bit value (b'nnnn').您可以使用带引号的字符串('ab')、十六进制字符串(X'6162'0x6162)或位值(b'nnnn')。

formatID is an unsigned integer.formatID是一个无符号整数。

The gtrid and bqual values are interpreted in bytes by the MySQL server's underlying XA support routines. gtridbqual值由MySQL服务器的底层XA支持例程以字节为单位进行解释。However, while an SQL statement containing an XA statement is being parsed, the server works with some specific character set. 但是,当解析包含XA语句的SQL语句时,服务器会处理某些特定的字符集。To be safe, write gtrid and bqual as hex strings.为了安全起见,将gtridbqual写为十六进制字符串。

xid values typically are generated by the Transaction Manager. xid值通常由事务管理器生成。Values generated by one TM must be different from values generated by other TMs. 一个TM生成的值必须不同于其他TM生成的值。A given TM must be able to recognize its own xid values in a list of values returned by the XA RECOVER statement.给定的TM必须能够在XA RECOVER语句返回的值列表中识别自己的xid值。

XA START xid starts an XA transaction with the given xid value. XA START xid使用给定的xid值来启动一个XA事务。Each XA transaction must have a unique xid value, so the value must not currently be used by another XA transaction. 每个XA事务必须具有唯一的xid值,因此该值当前不能由另一个XA事务使用。Uniqueness is assessed using the gtrid and bqual values. 使用gtridbqual值评估唯一性。All following XA statements for the XA transaction must be specified using the same xid value as that given in the XA START statement. 必须使用与XA START语句中给定值相同的xid值指定XA事务的所有以下XA语句。If you use any of those statements but specify an xid value that does not correspond to some existing XA transaction, an error occurs.如果使用这些语句中的任何一条,但指定的xid值与某些现有XA事务不对应,则会发生错误。

One or more XA transactions can be part of the same global transaction. 一个或多个XA事务可以是同一全局事务的一部分。All XA transactions within a given global transaction must use the same gtrid value in the xid value. 给定全局事务中的所有XA事务必须在xid值中使用相同的gtrid值。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. 出于这个原因,gtrid值必须是全局唯一的,这样就不会对给定XA事务属于哪个全局事务产生歧义。The bqual part of the xid value must be different for each XA transaction within a global transaction. 对于全局事务中的每个XA事务,xid值的bqual部分必须不同。(The requirement that bqual values be different is a limitation of the current MySQL XA implementation. It is not part of the XA specification.)(要求bqual值不同是当前MySQL XA实现的一个限制。它不是XA规范的一部分。)

The XA RECOVER statement returns information for those XA transactions on the MySQL server that are in the PREPARED state. XA RECOVER语句返回MySQL服务器上处于PREPARED状态的XA事务的信息。(See Section 13.3.8.2, “XA Transaction States”.) (请参阅第13.3.8.2节,“XA事务状态”。)The output includes a row for each such XA transaction on the server, regardless of which client started it.输出包括服务器上每个此类XA事务的一行,而不管是哪个客户端启动的。

XA RECOVER requires the XA_RECOVER_ADMIN privilege. XA RECOVER需要XA_RECOVER_ADMIN权限。This privilege requirement prevents users from discovering the XID values for outstanding prepared XA transactions other than their own. 此权限要求防止用户发现自己事务以外的未完成准备XA事务的XID值。It does not affect normal commit or rollback of an XA transaction because the user who started it knows its XID.它不会影响XA事务的正常提交或回滚,因为启动它的用户知道它的XID。

XA RECOVER output rows look like this (for an example xid value consisting of the parts 'abc', 'def', and 7):XA RECOVER输出行如下所示(例如,由'abc''def'7部分组成的xid值):

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

The output columns have the following meanings:输出列具有以下含义:

XID values may contain nonprintable characters. XID值可能包含不可打印的字符。XA RECOVER permits an optional CONVERT XID clause so that clients can request XID values in hexadecimal.XA RECOVER允许一个可选的CONVERT XID子句,以便客户端可以请求十六进制的XID值。