13.3.8.2 XA Transaction StatesXA事务状态

An XA transaction progresses through the following states:XA事务通过以下状态进行:

  1. Use XA START to start an XA transaction and put it in the ACTIVE state.使用XA START启动XA事务并将其置于ACTIVE状态。

  2. For an ACTIVE XA transaction, issue the SQL statements that make up the transaction, and then issue an XA END statement. 对于ACTIVEXA事务,发出组成事务的SQL语句,然后发出XA END语句。XA END puts the transaction in the IDLE state.XA END将事务置于空闲状态。

  3. For an IDLE XA transaction, you can issue either an XA PREPARE statement or an XA COMMIT ... ONE PHASE statement:对于IDLE(空闲)XA事务,您可以发出XA PREPARE语句或XA COMMIT ... ONE PHASE语句:

    • XA PREPARE puts the transaction in the PREPARED state. XA PREPARE将事务置于PREPARED状态。An XA RECOVER statement at this point includes the transaction's xid value in its output, because XA RECOVER lists all XA transactions that are in the PREPARED state.此时的XA RECOVER语句在其输出中包含事务的xid值,因为XA RECOVER列出了处于PREPARED状态的所有XA事务。

    • XA COMMIT ... ONE PHASE prepares and commits the transaction. XA COMMIT ... ONE PHASE准备并提交事务。The xid value is not listed by XA RECOVER because the transaction terminates.XA RECOVER未列出xid值,因为事务终止。

  4. For a PREPARED XA transaction, you can issue an XA COMMIT statement to commit and terminate the transaction, or XA ROLLBACK to roll back and terminate the transaction.对于PREPAREDXA事务,可以发出XA COMMIT语句来提交和终止事务,或者发出XA ROLLBACK语句来回滚和终止事务。

Here is a simple XA transaction that inserts a row into a table as part of a global transaction:下面是一个简单的XA事务,它将行作为全局事务的一部分插入表中:

mysql> XA START 'xatest';
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO mytable (i) VALUES(10);
Query OK, 1 row affected (0.04 sec)

mysql> XA END 'xatest';
Query OK, 0 rows affected (0.00 sec)

mysql> XA PREPARE 'xatest';
Query OK, 0 rows affected (0.00 sec)

mysql> XA COMMIT 'xatest';
Query OK, 0 rows affected (0.00 sec)

Within the context of a given client connection, XA transactions and local (non-XA) transactions are mutually exclusive. 在给定客户端连接的上下文中,XA事务和本地(非XA)事务是互斥的。For example, if XA START has been issued to begin an XA transaction, a local transaction cannot be started until the XA transaction has been committed or rolled back. 例如,如果发出XA START以开始XA事务,则在提交或回滚XA事务之前,无法启动本地事务。Conversely, if a local transaction has been started with START TRANSACTION, no XA statements can be used until the transaction has been committed or rolled back.相反,如果本地事务是用START TRANSACTION启动的,则在提交或回滚事务之前,不能使用XA语句。

If an XA transaction is in the ACTIVE state, you cannot issue any statements that cause an implicit commit. 如果XA事务处于ACTIVE状态,则不能发出任何导致隐式提交的语句。That would violate the XA contract because you could not roll back the XA transaction. 这将违反XA合同,因为您无法回滚XA事务。Trying to execute such a statement raises the following error:尝试执行此类语句会引发以下错误:

ERROR 1399 (XAE07): XAER_RMFAIL: The command cannot be executed
when global transaction is in the ACTIVE state

Statements to which the preceding remark applies are listed at Section 13.3.3, “Statements That Cause an Implicit Commit”.第13.3.3节,“导致隐式提交的语句”中列出了上述备注适用的语句。