13.3.1 START TRANSACTION, COMMIT, and ROLLBACK StatementsSTART TRANSACTION语句、COMMIT语句和ROLLBACK语句

START TRANSACTION
    [transaction_characteristic [, transaction_characteristic] ...]
transaction_characteristic: {
    WITH CONSISTENT SNAPSHOT
  | READ WRITE
  | READ ONLY
}

BEGIN [WORK]
COMMIT [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
ROLLBACK [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
SET autocommit = {0 | 1}

These statements provide control over use of transactions:这些报表提供了对事务使用的控制:

By default, MySQL runs with autocommit mode enabled. 默认情况下,MySQL在启用自动提交模式的情况下运行。This means that, when not otherwise inside a transaction, each statement is atomic, as if it were surrounded by START TRANSACTION and COMMIT. 这意味着,当不是在事务内部时,每个语句都是原子的,就好像它被START TRANSACTIONCOMMIT包围一样。You cannot use ROLLBACK to undo the effect; however, if an error occurs during statement execution, the statement is rolled back.不能使用ROLLBACK撤消效果;但是,如果在语句执行期间发生错误,则会回滚该语句。

To disable autocommit mode implicitly for a single series of statements, use the START TRANSACTION statement:要对一系列语句隐式禁用自动提交模式,请使用START TRANSACTION语句:

START TRANSACTION;
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=@A WHERE type=1;
COMMIT;

With START TRANSACTION, autocommit remains disabled until you end the transaction with COMMIT or ROLLBACK. 使用START TRANSACTION,自动提交将保持禁用状态,直到您使用COMMITROLLBACK结束事务。The autocommit mode then reverts to its previous state.然后,自动提交模式将恢复到以前的状态。

START TRANSACTION permits several modifiers that control transaction characteristics. START TRANSACTION允许几个控制事务特征的修饰符。To specify multiple modifiers, separate them by commas.要指定多个修改器,请用逗号分隔它们。

Important重要

Many APIs used for writing MySQL client applications (such as JDBC) provide their own methods for starting transactions that can (and sometimes should) be used instead of sending a START TRANSACTION statement from the client. 许多用于编写MySQL客户机应用程序(如JDBC)的API都提供了自己的启动事务的方法,可以(有时应该)使用这些方法,而不是从客户机发送START TRANSACTION语句。See Chapter 29, Connectors and APIs, or the documentation for your API, for more information.有关更多信息,请参阅第29章,“连接器和API”或API文档。

To disable autocommit mode explicitly, use the following statement:要显式禁用自动提交模式,请使用以下语句:

SET autocommit=0;

After disabling autocommit mode by setting the autocommit variable to zero, changes to transaction-safe tables (such as those for InnoDB or NDB) are not made permanent immediately. 通过将autocommit变量设置为零禁用自动提交模式后,对事务安全表(如InnoDBNDB表)的更改不会立即永久化。You must use COMMIT to store your changes to disk or ROLLBACK to ignore the changes.必须使用COMMIT将更改存储到磁盘,或使用ROLLBACK忽略更改。

autocommit is a session variable and must be set for each session. autocommit是一个会话变量,必须为每个会话设置。To disable autocommit mode for each new connection, see the description of the autocommit system variable at Section 5.1.8, “Server System Variables”.要为每个新连接禁用自动提交模式,请参阅第5.1.8节,“服务器系统变量”中的autocommit系统变量说明。

BEGIN and BEGIN WORK are supported as aliases of START TRANSACTION for initiating a transaction. BEGINBEGIN WORK作为启动事务的“开始事务”别名受支持。START TRANSACTION is standard SQL syntax, is the recommended way to start an ad-hoc transaction, and permits modifiers that BEGIN does not.START TRANSACTION是标准的SQL语法,是启动特别事务的推荐方法,并且允许BEGIN不允许的修饰符。

The BEGIN statement differs from the use of the BEGIN keyword that starts a BEGIN ... END compound statement. BEGIN语句不同于使用BEGIN关键字启动BEGIN ... END复合语句。The latter does not begin a transaction. 后者不启动事务。See Section 13.6.1, “BEGIN ... END Compound Statement”.请参阅第13.6.1节,“BEGIN ... END复合语句”

Note注意

Within all stored programs (stored procedures and functions, triggers, and events), the parser treats BEGIN [WORK] as the beginning of a BEGIN ... END block. 在所有存储程序(存储过程和函数、触发器和事件)中,解析器将BEGIN [WORK]视为BEGIN ... END块的开头。Begin a transaction in this context with START TRANSACTION instead.在该上下文中改用START TRANSACTION开始事务。

The optional WORK keyword is supported for COMMIT and ROLLBACK, as are the CHAIN and RELEASE clauses. COMMITROLLBACK支持可选的WORK关键字,CHAINRELEASE子句也是如此。CHAIN and RELEASE can be used for additional control over transaction completion. CHAINRELEASE可用于对事务完成的额外控制。The value of the completion_type system variable determines the default completion behavior. completion_type系统变量的值决定默认的完成行为。See Section 5.1.8, “Server System Variables”.请参阅第5.1.8节,“服务器系统变量”

The AND CHAIN clause causes a new transaction to begin as soon as the current one ends, and the new transaction has the same isolation level as the just-terminated transaction. AND CHAIN子句使新事务在当前事务结束时立即开始,并且新事务与刚刚终止的事务具有相同的隔离级别。The new transaction also uses the same access mode (READ WRITE or READ ONLY) as the just-terminated transaction. 新事务还使用与刚刚终止的事务相同的访问模式(READ WRITEREAD ONLY)。The RELEASE clause causes the server to disconnect the current client session after terminating the current transaction. RELEASE子句导致服务器在终止当前事务后断开当前客户端会话。Including the NO keyword suppresses CHAIN or RELEASE completion, which can be useful if the completion_type system variable is set to cause chaining or release completion by default.包含NO关键字将抑制CHAINRELEASE完成,如果completion_type系统变量默认设置为导致链或发布完成,则此选项非常有用。

Beginning a transaction causes any pending transaction to be committed. 开始事务会导致提交任何挂起的事务。See Section 13.3.3, “Statements That Cause an Implicit Commit”, for more information.有关更多信息,请参阅第13.3.3节,“导致隐式提交的语句”

Beginning a transaction also causes table locks acquired with LOCK TABLES to be released, as though you had executed UNLOCK TABLES. 开始一个事务也会导致释放使用LOCK TABLES获取的表锁,就像执行了UNLOCK TABLES一样。Beginning a transaction does not release a global read lock acquired with FLUSH TABLES WITH READ LOCK.启动事务不会释放使用FLUSH TABLES WITH READ LOCK获取的全局读锁。

For best results, transactions should be performed using only tables managed by a single transaction-safe storage engine. 为了获得最佳结果,应仅使用由单个事务安全存储引擎管理的表执行事务。Otherwise, the following problems can occur:否则,可能会出现以下问题:

Each transaction is stored in the binary log in one chunk, upon COMMIT. 在提交时,每个事务都以一个块的形式存储在二进制日志中。Transactions that are rolled back are not logged. 不会记录回滚的事务。(Exception: Modifications to nontransactional tables cannot be rolled back. 例外:无法回滚对非事务表的修改。If a transaction that is rolled back includes modifications to nontransactional tables, the entire transaction is logged with a ROLLBACK statement at the end to ensure that modifications to the nontransactional tables are replicated.) 如果回滚的事务包括对非事务表的修改,则整个事务将在末尾使用ROLLBACK语句进行记录,以确保复制对非事务表的修改。)See Section 5.4.4, “The Binary Log”.请参阅第5.4.4节,“二进制日志”

You can change the isolation level or access mode for transactions with the SET TRANSACTION statement. 您可以使用SET TRANSACTION语句更改事务的隔离级别或访问模式。See Section 13.3.7, “SET TRANSACTION Statement”.请参阅第13.3.7节,“设置交易报表”

Rolling back can be a slow operation that may occur implicitly without the user having explicitly asked for it (for example, when an error occurs). 回滚可能是一个缓慢的操作,可能在用户没有明确请求的情况下隐式发生(例如,当发生错误时)。Because of this, SHOW PROCESSLIST displays Rolling back in the State column for the session, not only for explicit rollbacks performed with the ROLLBACK statement but also for implicit rollbacks.因此,SHOW PROCESSLIST在会话的State列中显示Rolling back<,不仅用于使用ROLLBACK语句执行的显式回滚,也用于隐式回滚。

Note注意

In MySQL 8.0, BEGIN, COMMIT, and ROLLBACK are not affected by --replicate-do-db or --replicate-ignore-db rules.在MySQL 8.0中,BEGINCOMMITROLLBACK不受--replicate-do-db--replicate-ignore-db规则的影响。

When InnoDB performs a complete rollback of a transaction, all locks set by the transaction are released. InnoDB执行事务的完全回滚时,事务设置的所有锁都将被释放。If a single SQL statement within a transaction rolls back as a result of an error, such as a duplicate key error, locks set by the statement are preserved while the transaction remains active. 如果事务中的单个SQL语句由于错误(如重复密钥错误)而回滚,则在事务保持活动状态时,将保留该语句设置的锁。This happens because InnoDB stores row locks in a format such that it cannot know afterward which lock was set by which statement.之所以会发生这种情况,是因为InnoDB以一种格式存储行锁,这样它就无法知道哪个锁是由哪个语句设置的。

If a SELECT statement within a transaction calls a stored function, and a statement within the stored function fails, that statement rolls back. 如果事务中的SELECT语句调用存储函数,而存储函数中的语句失败,则该语句将回滚。If ROLLBACK is executed for the transaction subsequently, the entire transaction rolls back.如果随后对事务执行ROLLBACK,则整个事务将回滚。