#### 13.3.8.2 XA Transaction States

 An XA transaction progresses through the following states:

1. Use [`XA START`](xa-statements.html) to start an XA transaction and put it in the `ACTIVE` state.

2. For an `ACTIVE` XA transaction, issue the SQL statements that make up the transaction, and then issue an [`XA END`](xa-statements.html) statement. [`XA END`](xa-statements.html) puts the transaction in the `IDLE` state.

3. For an `IDLE` XA transaction, you can issue either an [`XA PREPARE`](xa-statements.html) statement or an `XA COMMIT ... ONE PHASE` statement:

   * [`XA PREPARE`](xa-statements.html) puts the transaction in the `PREPARED` state. An [`XA RECOVER`](xa-statements.html) statement at this point includes the transaction's *`xid`* value in its output, because [`XA RECOVER`](xa-statements.html) lists all XA transactions that are in the `PREPARED` state.

   * `XA COMMIT ... ONE PHASE` prepares and commits the transaction. The *`xid`* value is not listed by [`XA RECOVER`](xa-statements.html) because the transaction terminates.

4. For a `PREPARED` XA transaction, you can issue an [`XA COMMIT`](xa-statements.html) statement to commit and terminate the transaction, or [`XA ROLLBACK`](xa-statements.html) to roll back and terminate the transaction.

 Here is a simple XA transaction that inserts a row into a table as part of a global transaction:

```
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)
```

 In MySQL 8.0.28 and earlier, within the context of a given client connection, XA transactions and local (non-XA) transactions are mutually exclusive. For example, if [`XA START`](xa-statements.html) has been issued to begin an XA transaction, a local transaction cannot be started until the XA transaction has been committed or rolled back. Conversely, if a local transaction has been started with [`START TRANSACTION`](commit.html), no XA statements can be used until the transaction has been committed or rolled back.

 MySQL 8.0.29 and later supports detached XA transactions, enabled by the [`xa_detach_on_prepare`](server-system-variables.html#sysvar_xa_detach_on_prepare) system variable (`ON` by default). Detached transactions are disconnected from the current session following execution of [`XA PREPARE`](xa-statements.html) (and can be committed or rolled back by another connection). This means that the current session is free to start a new local transaction or XA transaction without having to wait for the prepared XA transaction to be committed or rolled back.

 When XA transactions are detached, a connection has no special knowledge of any XA transaction that it has prepared. If the current session tries to commit or roll back a given XA transaction (even one which it prepared) after another connection has already done so, the attempt is rejected with an invalid XID error ([`ER_XAER_NOTA`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_xaer_nota)) since the requested *`xid`* no longer exists.

Note

 Detached XA transactions cannot use temporary tables.

 When detached XA transactions are disabled (`xa_detach_on_prepare` set to `OFF`), an XA transaction remains connected until it is committed or rolled back by the originating connection, as described previously for MySQL 8.0.28 and earlier. Disabling detached XA transactions is not recommended for a MySQL server instance used in group replication; see [Server Instance Configuration](group-replication-requirements.html#group-replication-configuration), for more information.

 If an XA transaction is in the `ACTIVE` state, you cannot issue any statements that cause an implicit commit. That would violate the XA contract because you could not roll back the XA transaction. 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”](implicit-commit.html).

