MySQL事务的默认隔离级别是'Repeatable Read'。
根据另一个 stackoverflow 问题( Difference between read commit and repeatable read ) "可重复读是一个更高的隔离级别,它除了读提交级别的保证外,还保证任何读取的数据都不能改变,如果事务再次读取相同的数据,它会在原地找到之前读取的数据,未更改,可供阅读。”
这是我的测试数据库;
mysql> select * from people;
+------+---------+
| name | howmany |
+------+---------+
| alex | 100 |
| bob | 100 |
+------+---------+
慢.sql
START TRANSACTION;
SELECT @new_val := howmany FROM people WHERE name = 'alex';
SELECT SLEEP(10);
SET @new_val = @new_val - 5;
UPDATE people SET howmany = @new_val WHERE name = 'alex';
COMMIT;
快速.sql
START TRANSACTION;
SELECT @new_val := howmany FROM people WHERE name = 'alex';
-- SELECT SLEEP(10);
SET @new_val = @new_val - 5;
UPDATE people SET howmany = @new_val WHERE name = 'alex';
COMMIT;
如果我运行 slow.sql,在它返回之前,我会多次运行 fast.sql。 fast.sql 将打印 95、90、85....
我认为可重复读隔离级别应该使 fast.sql 无法运行或者我误解了“可重复读”。
我在 Ubuntu 16.10 上运行 MySQL 5.7。
非常感谢。
最佳答案
如果没有错,那么 Repeatable Read
谈论的是同一事务中的一致性读取,而不是其他事务中的一致性读取。来自 MySQL Documentation
- REPEATABLE READ
This is the default isolation level for InnoDB. Consistent reads within the same transaction read the snapshot established by the first read. This means that if you issue several plain (nonlocking) SELECT statements within the same transaction, these SELECT statements are consistent also with respect to each other.
关于MySQL 'REPEATABLE READ' 事务异常行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42668158/