13.3.7 SET TRANSACTION Statement语句

SET [GLOBAL | SESSION] TRANSACTION
transaction_characteristic [, transaction_characteristic] ...
transaction_characteristic: {
    ISOLATION LEVEL level
  | access_mode
}
level: {
     REPEATABLE READ
   | READ COMMITTED
   | READ UNCOMMITTED
   | SERIALIZABLE
}
access_mode: {
     READ WRITE
   | READ ONLY
}

This statement specifies transaction characteristics. 此语句指定事务特征。It takes a list of one or more characteristic values separated by commas. 它采用一个或多个由逗号分隔的特征值列表。Each characteristic value sets the transaction isolation level or access mode. 每个特征值设置事务隔离级别或访问模式。The isolation level is used for operations on InnoDB tables. 隔离级别用于InnoDB表上的操作。The access mode specifies whether transactions operate in read/write or read-only mode.访问模式指定事务是以读/写模式还是只读模式运行。

In addition, SET TRANSACTION can include an optional GLOBAL or SESSION keyword to indicate the scope of the statement.此外,SET TRANSACTION还可以包括可选的GLOBALSESSION关键字,以指示语句的范围。

Transaction Isolation Levels事务隔离级别

To set the transaction isolation level, use an ISOLATION LEVEL level clause. 要设置事务隔离级别,请使用ISOLATION LEVEL level子句。It is not permitted to specify multiple ISOLATION LEVEL clauses in the same SET TRANSACTION statement.不允许在同一SET TRANSACTION语句中指定多个ISOLATION LEVEL子句。

The default isolation level is REPEATABLE READ. 默认隔离级别为REPEATABLE READOther permitted values are READ COMMITTED, READ UNCOMMITTED, and SERIALIZABLE. 其他允许的值包括READ COMMITTEDREAD UNCOMMITTEDSERIALIZABLEFor information about these isolation levels, see Section 15.7.2.1, “Transaction Isolation Levels”.有关这些隔离级别的信息,请参阅第15.7.2.1节,“事务隔离级别”

Transaction Access Mode事务访问模式

To set the transaction access mode, use a READ WRITE or READ ONLY clause. 要设置事务访问模式,请使用READ WRITEREAD ONLY子句。It is not permitted to specify multiple access-mode clauses in the same SET TRANSACTION statement.不允许在同一SET TRANSACTION语句中指定多访问模式子句。

By default, a transaction takes place in read/write mode, with both reads and writes permitted to tables used in the transaction. 默认情况下,事务以读/写模式进行,同时允许对事务中使用的表进行读写。This mode may be specified explicitly using SET TRANSACTION with an access mode of READ WRITE.此模式可以使用具有读写访问模式的SET TRANSACTION显式指定。

If the transaction access mode is set to READ ONLY, changes to tables are prohibited. 如果事务访问模式设置为READ ONLY,则禁止更改表。This may enable storage engines to make performance improvements that are possible when writes are not permitted.这可能使存储引擎能够在不允许写入时实现性能改进。

In read-only mode, it remains possible to change tables created with the TEMPORARY keyword using DML statements. 在只读模式下,仍然可以使用DML语句更改使用TEMPORARY关键字创建的表。Changes made with DDL statements are not permitted, just as with permanent tables.与永久表一样,不允许使用DDL语句进行更改。

The READ WRITE and READ ONLY access modes also may be specified for an individual transaction using the START TRANSACTION statement.还可以使用START TRANSACTION语句为单个事务指定READ WRITEREAD ONLY访问模式。

Transaction Characteristic Scope事务特征范围

You can set transaction characteristics globally, for the current session, or for the next transaction only:您可以为当前会话或仅为下一个事务全局设置事务特征:

  • With the GLOBAL keyword:使用GLOBAL关键字:

    • The statement applies globally for all subsequent sessions.该语句适用于所有后续会话。

    • Existing sessions are unaffected.现有会话不受影响。

  • With the SESSION keyword:使用SESSION关键字:

    • The statement applies to all subsequent transactions performed within the current session.该语句适用于当前会话中执行的所有后续事务。

    • The statement is permitted within transactions, but does not affect the current ongoing transaction.该声明允许在交易中使用,但不影响当前正在进行的交易。

    • If executed between transactions, the statement overrides any preceding statement that sets the next-transaction value of the named characteristics.如果在事务之间执行,则该语句将覆盖设置命名特征的下一个事务值的任何先前语句。

  • Without any SESSION or GLOBAL keyword:没有任何SESSIONGLOBAL关键字:

    • The statement applies only to the next single transaction performed within the session.该语句仅适用于会话中执行的下一个事务。

    • Subsequent transactions revert to using the session value of the named characteristics.后续事务恢复为使用命名特征的会话值。

    • The statement is not permitted within transactions:以下交易中不允许使用该声明:

      mysql> START TRANSACTION;
      Query OK, 0 rows affected (0.02 sec)
      
      mysql> SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
      ERROR 1568 (25001): Transaction characteristics can't be changed
      while a transaction is in progress

A change to global transaction characteristics requires the CONNECTION_ADMIN privilege (or the deprecated SUPER privilege). 对全局事务特征的更改需要CONNECTION_ADMIN权限(或不推荐使用的SUPER权限)。Any session is free to change its session characteristics (even in the middle of a transaction), or the characteristics for its next transaction (prior to the start of that transaction).任何会话都可以自由地改变其会话特性(即使在事务的中间),也可以改变其下一个事务的特征(在该事务开始之前)。

To set the global isolation level at server startup, use the --transaction-isolation=level option on the command line or in an option file. 要在服务器启动时设置全局隔离级别,请在命令行或选项文件中使用---transaction-isolation=level选项。Values of level for this option use dashes rather than spaces, so the permissible values are READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, or SERIALIZABLE.此选项的level值使用破折号而不是空格,因此允许的值为READ-UNCOMMITTEDREAD-COMMITTEDREPEATABLE-READSERIALIZABLE

Similarly, to set the global transaction access mode at server startup, use the --transaction-read-only option. 类似地,要在服务器启动时设置全局事务访问模式,请使用--transaction-read-only选项。The default is OFF (read/write mode) but the value can be set to ON for a mode of read only.默认值为OFF(读/写模式),但对于只读模式,该值可以设置为ON

For example, to set the isolation level to REPEATABLE READ and the access mode to READ WRITE, use these lines in the [mysqld] section of an option file:例如,要将隔离级别设置为REPEATABLE READ,将访问模式设置为READ WRITE,请在选项文件的[mysqld]部分使用以下行:

[mysqld]
transaction-isolation = REPEATABLE-READ
transaction-read-only = OFF

At runtime, characteristics at the global, session, and next-transaction scope levels can be set indirectly using the SET TRANSACTION statement, as described previously. 在运行时,可以使用SET TRANSACTION语句间接设置全局、会话和下一个事务作用域级别的特征,如前所述。They can also be set directly using the SET statement to assign values to the transaction_isolation and transaction_read_only system variables:也可以使用SET语句直接设置它们,以便为系统变量transaction_isolationtransaction_read_only赋值:

  • SET TRANSACTION permits optional GLOBAL and SESSION keywords for setting transaction characteristics at different scope levels.SET TRANSACTION允许在不同范围级别设置事务特征的可选的GLOBAL关键字和SESSION关键字。

  • The SET statement for assigning values to the transaction_isolation and transaction_read_only system variables has syntaxes for setting these variables at different scope levels.用于为系统变量transaction_isolationtransaction_read_only赋值的SET语句具有用于在不同范围级别设置这些变量的语法。

The following tables show the characteristic scope level set by each SET TRANSACTION and variable-assignment syntax.下表显示了每个SET TRANSACTION和变量分配语法设置的特征范围级别。

Table 13.9 SET TRANSACTION Syntax for Transaction Characteristics为事务特征设置事务语法

Syntax语法Affected Characteristic Scope影响特征范围
SET GLOBAL TRANSACTION transaction_characteristicGlobal全局
SET SESSION TRANSACTION transaction_characteristicSession会话
SET TRANSACTION transaction_characteristicNext transaction only仅限下一个事务

Table 13.10 SET Syntax for Transaction Characteristics设置事务特征的语法

Syntax语法Affected Characteristic Scope影响特征范围
SET GLOBAL var_name = valueGlobal
SET @@GLOBAL.var_name = valueGlobal
SET PERSIST var_name = valueGlobal
SET @@PERSIST.var_name = valueGlobal
SET PERSIST_ONLY var_name = valueNo runtime effect
SET @@PERSIST_ONLY.var_name = valueNo runtime effect
SET SESSION var_name = valueSession
SET @@SESSION.var_name = valueSession
SET var_name = valueSession
SET @@var_name = valueNext transaction only

It is possible to check the global and session values of transaction characteristics at runtime:可以在运行时检查事务特征的全局值和会话值:

SELECT @@GLOBAL.transaction_isolation, @@GLOBAL.transaction_read_only;
SELECT @@SESSION.transaction_isolation, @@SESSION.transaction_read_only;