SET TRANSACTION

The SET TRANSACTION statement can be used to change the current isolation level on a GLOBAL or SESSION basis. This syntax is an alternative to SET transaction_isolation='new-value' and is included for compatibility with both MySQL, and the SQL standards.

Synopsis

SetStmt

SET TRANSACTION - 图1

TransactionChars

SET TRANSACTION - 图2

IsolationLevel

SET TRANSACTION - 图3

AsOfClause

SET TRANSACTION - 图4

  1. SetStmt ::=
  2. 'SET' ( VariableAssignmentList |
  3. 'PASSWORD' ('FOR' Username)? '=' PasswordOpt |
  4. ( 'GLOBAL'| 'SESSION' )? 'TRANSACTION' TransactionChars |
  5. 'CONFIG' ( Identifier | stringLit) ConfigItemName EqOrAssignmentEq SetExpr )
  6. TransactionChars ::=
  7. ( 'ISOLATION' 'LEVEL' IsolationLevel | 'READ' 'WRITE' | 'READ' 'ONLY' AsOfClause? )
  8. IsolationLevel ::=
  9. ( 'REPEATABLE' 'READ' | 'READ' ( 'COMMITTED' | 'UNCOMMITTED' ) | 'SERIALIZABLE' )
  10. AsOfClause ::=
  11. ( 'AS' 'OF' 'TIMESTAMP' Expression)

Examples

  1. mysql> SHOW SESSION VARIABLES LIKE 'transaction_isolation';
  2. +-----------------------+-----------------+
  3. | Variable_name | Value |
  4. +-----------------------+-----------------+
  5. | transaction_isolation | REPEATABLE-READ |
  6. +-----------------------+-----------------+
  7. 1 row in set (0.00 sec)
  8. mysql> SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
  9. Query OK, 0 rows affected (0.00 sec)
  10. mysql> SHOW SESSION VARIABLES LIKE 'transaction_isolation';
  11. +-----------------------+----------------+
  12. | Variable_name | Value |
  13. +-----------------------+----------------+
  14. | transaction_isolation | READ-COMMITTED |
  15. +-----------------------+----------------+
  16. 1 row in set (0.01 sec)
  17. mysql> SET SESSION transaction_isolation = 'REPEATABLE-READ';
  18. Query OK, 0 rows affected (0.00 sec)
  19. mysql> SHOW SESSION VARIABLES LIKE 'transaction_isolation';
  20. +-----------------------+-----------------+
  21. | Variable_name | Value |
  22. +-----------------------+-----------------+
  23. | transaction_isolation | REPEATABLE-READ |
  24. +-----------------------+-----------------+
  25. 1 row in set (0.00 sec)

MySQL compatibility

  • TiDB supports the ability to set a transaction as read-only in syntax only.
  • The isolation levels READ-UNCOMMITTED and SERIALIZABLE are not supported.
  • The REPEATABLE-READ isolation level is achieved through using the snapshot isolation technology, which is partly compatible with MySQL.
  • In pessimistic transactions, TiDB supports two isolation levels compatible with MySQL: REPEATABLE-READ and READ-COMMITTED. For a detailed description, see Isolation Levels.

See also