SET TRANSACTION

SET TRANSACTION 语句用于在 GLOBALSESSION 的基础上更改当前的隔离级别,是 SET transaction_isolation ='new-value' 的替代语句,提供 MySQL 和 SQL 标准的兼容性。

语法图

SetStmt:

SetStmt

TransactionChar:

TransactionChar

IsolationLevel:

IsolationLevel

示例

  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 兼容性

  • TiDB 支持仅在语法中将事务设置为只读的功能。
  • 不支持隔离级别 READ-UNCOMMITTEDSERIALIZABLE
  • 隔离级别 REPEATABLE-READ 在技术上属于快照隔离(Snapshot Isolation)。在 TiDB 中称为 REPEATABLE-READ 是为了和 MySQL 保持一致。

另请参阅