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:这些报表提供了对事务使用的控制:
START TRANSACTION
or BEGIN
start a new transaction.START TRANSACTION
或BEGIN
启动了一个新事务。
COMMIT
commits the current transaction, making its changes permanent.COMMIT
提交当前事务,使其更改永久化。
ROLLBACK
rolls back the current transaction, canceling its changes.ROLLBACK
回滚当前事务,取消其更改。
SET autocommit
disables or enables the default autocommit mode for the current session.SET autocommit
为当前会话禁用或启用默认自动提交模式。
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 TRANSACTION
和COMMIT
包围一样。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
,自动提交将保持禁用状态,直到您使用COMMIT
或ROLLBACK
结束事务。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.要指定多个修改器,请用逗号分隔它们。
The WITH CONSISTENT SNAPSHOT
modifier starts a consistent read for storage engines that are capable of it. WITH CONSISTENT SNAPSHOT
修饰符为能够进行一致读取的存储引擎启动一致读取。This applies only to 这只适用于InnoDB
. InnoDB
。The effect is the same as issuing a 其效果与从任何START TRANSACTION
followed by a SELECT
from any InnoDB
table. InnoDB
表中发出一个START TRANSACTION
,后跟一个SELECT
相同。See Section 15.7.2.3, “Consistent Nonlocking Reads”. 请参阅第15.7.2.3节,“一致非锁定读数”。The WITH CONSISTENT SNAPSHOT
modifier does not change the current transaction isolation level, so it provides a consistent snapshot only if the current isolation level is one that permits a consistent read. WITH CONSISTENT SNAPSHOT
修饰符不会更改当前事务隔离级别,因此仅当当前隔离级别允许一致读取时,它才提供一致快照。The only isolation level that permits a consistent read is 唯一允许一致读取的隔离级别是REPEATABLE READ
. REPEATABLE READ
。For all other isolation levels, the 对于所有其他隔离级别,将忽略WITH CONSISTENT SNAPSHOT
clause is ignored. WITH CONSTRIATED SNAPSHOT
子句。A warning is generated when the 忽略WITH CONSISTENT SNAPSHOT
clause is ignored.WITH CONSTRIATED SNAPSHOT
子句时将生成警告。
The READ WRITE
and READ ONLY
modifiers set the transaction access mode. READ WRITE
和READ ONLY
修饰符设置事务访问模式。They permit or prohibit changes to tables used in the transaction. 它们允许或禁止更改事务中使用的表。The READ ONLY
restriction prevents the transaction from modifying or locking both transactional and nontransactional tables that are visible to other transactions; the transaction can still modify or lock temporary tables.READ ONLY
限制防止事务修改或锁定对其他事务可见的事务表和非事务表;事务仍然可以修改或锁定临时表。
MySQL enables extra optimizations for queries on 当事务已知为只读时,MySQL支持对InnoDB
tables when the transaction is known to be read-only. InnoDB
表上的查询进行额外优化。Specifying 指定READ ONLY
ensures these optimizations are applied in cases where the read-only status cannot be determined automatically. READ ONLY
可确保在无法自动确定只读状态的情况下应用这些优化。See Section 8.5.3, “Optimizing InnoDB Read-Only Transactions” for more information.有关更多信息,请参阅第8.5.3节,“优化InnoDB只读事务”。
If no access mode is specified, the default mode applies. Unless the default has been changed, it is read/write. 如果未指定访问模式,则应用默认模式。除非已更改默认值,否则它是读/写的。It is not permitted to specify both 不允许在同一语句中同时指定READ WRITE
and READ ONLY
in the same statement.READ WRITE
和READ ONLY
。
In read-only mode, it remains possible to change tables created with the 在只读模式下,仍然可以使用DML语句更改使用TEMPORARY
keyword using DML statements. TEMPORARY
关键字创建的表。Changes made with DDL statements are not permitted, just as with permanent tables.与永久表一样,不允许使用DDL语句进行更改。
For additional information about transaction access mode, including ways to change the default mode, see Section 13.3.7, “SET TRANSACTION Statement”.有关事务访问模式的更多信息,包括更改默认模式的方法,请参阅第13.3.7节,“设置事务语句”。
If the 如果启用了read_only
system variable is enabled, explicitly starting a transaction with START TRANSACTION READ WRITE
requires the CONNECTION_ADMIN
privilege (or the deprecated SUPER
privilege).read_only
系统变量,则使用START TRANSACTION READ WRITE
显式启动事务需要CONNECTION_ADMIN
权限(或不推荐使用的SUPER
权限)。
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 许多用于编写MySQL客户机应用程序(如JDBC)的API都提供了自己的启动事务的方法,可以(有时应该)使用这些方法,而不是从客户机发送START TRANSACTION
statement from the client. 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
变量设置为零禁用自动提交模式后,对事务安全表(如InnoDB
或NDB
表)的更改不会立即永久化。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 要为每个新连接禁用自动提交模式,请参阅第5.1.8节,“服务器系统变量”中的autocommit
system variable at Section 5.1.8, “Server System Variables”.autocommit
系统变量说明。
BEGIN
and BEGIN WORK
are supported as aliases of START TRANSACTION
for initiating a transaction. BEGIN
和BEGIN 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复合语句”。
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. COMMIT
和ROLLBACK
支持可选的WORK
关键字,CHAIN
和RELEASE
子句也是如此。CHAIN
and RELEASE
can be used for additional control over transaction completion. CHAIN
和RELEASE
可用于对事务完成的额外控制。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 WRITE
或READ 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
关键字将抑制CHAIN
或RELEASE
完成,如果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:否则,可能会出现以下问题:
If you use tables from more than one transaction-safe storage engine (such as 如果使用多个事务安全存储引擎(如InnoDB
), and the transaction isolation level is not SERIALIZABLE
, it is possible that when one transaction commits, another ongoing transaction that uses the same tables sees only some of the changes made by the first transaction. InnoDB
)中的表,并且事务隔离级别不可序列化,则当一个事务提交时,另一个使用相同表的正在进行的事务可能只看到第一个事务所做的部分更改。That is, the atomicity of transactions is not guaranteed with mixed engines and inconsistencies can result. 也就是说,混合引擎不能保证事务的原子性,并且可能导致不一致。(If mixed-engine transactions are infrequent, you can use (如果混合引擎事务很少出现,则可以根据需要使用SET TRANSACTION ISOLATION LEVEL
to set the isolation level to SERIALIZABLE
on a per-transaction basis as necessary.)SET TRANSACTION ISOLATION LEVEL
将隔离级别设置为可在每个事务的基础上序列化。)
If you use tables that are not transaction-safe within a transaction, changes to those tables are stored at once, regardless of the status of autocommit mode.如果在事务中使用非事务安全的表,则对这些表的更改将立即存储,而与自动提交模式的状态无关。
If you issue a 如果在更新事务中的非事务表后发出ROLLBACK
statement after updating a nontransactional table within a transaction, an ER_WARNING_NOT_COMPLETE_ROLLBACK
warning occurs. ROLLBACK
语句,则会出现ER_WARNING_NOT_COMPLETE_ROLLBACK
警告。Changes to transaction-safe tables are rolled back, but not changes to nontransaction-safe tables.回滚对事务安全表的更改,但不回滚对非事务安全表的更改。
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
语句执行的显式回滚,也用于隐式回滚。
In MySQL 8.0, 在MySQL 8.0中,BEGIN
, COMMIT
, and ROLLBACK
are not affected by --replicate-do-db
or --replicate-ignore-db
rules.BEGIN
、COMMIT
和ROLLBACK
不受--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
,则整个事务将回滚。