SET [GLOBAL | SESSION] TRANSACTIONtransaction_characteristic
[,transaction_characteristic
] ...transaction_characteristic
: { ISOLATION LEVELlevel
|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
还可以包括可选的GLOBAL
或SESSION
关键字,以指示语句的范围。
To set the transaction isolation level, use an 要设置事务隔离级别,请使用ISOLATION LEVEL
clause. level
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 READ
。Other permitted values are 其他允许的值包括READ COMMITTED
, READ UNCOMMITTED
, and SERIALIZABLE
. READ COMMITTED
、READ UNCOMMITTED
和SERIALIZABLE
。For information about these isolation levels, see Section 15.7.2.1, “Transaction Isolation Levels”.有关这些隔离级别的信息,请参阅第15.7.2.1节,“事务隔离级别”。
To set the transaction access mode, use a 要设置事务访问模式,请使用READ WRITE
or READ ONLY
clause. READ WRITE
或READ 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 在只读模式下,仍然可以使用DML语句更改使用TEMPORARY
keyword using DML statements. 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 WRITE
和READ ONLY
访问模式。
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:SESSION
或GLOBAL
关键字:
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=
option on the command line or in an option file. level
--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-UNCOMMITTED
、READ-COMMITTED
、REPEATABLE-READ
或SERIALIZABLE
。
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_isolation
和transaction_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_isolation
和transaction_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为事务特征设置事务语法
SET GLOBAL TRANSACTION | |
SET SESSION TRANSACTION | |
SET TRANSACTION |
Table 13.10 SET Syntax for Transaction Characteristics设置事务特征的语法
SET GLOBAL | Global |
SET @@GLOBAL. | Global |
SET PERSIST | Global |
SET @@PERSIST. | Global |
SET PERSIST_ONLY | No runtime effect |
SET @@PERSIST_ONLY. | No runtime effect |
SET SESSION | Session |
SET @@SESSION. | Session |
SET | Session |
SET @@ | Next 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;