MySQL 'REPEATABLE READ' 事务异常行为

标签 mysql sql transactions

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/

相关文章:

mysql - group by 之后的表达式字段=xxx

PHP 不向数据库提交用户详细信息?

php - 如何将信用卡付款整合到我的网站?

mysql - 我应该在我的日期字段上放置一个索引吗?

mysql - 带添加的环回更新

sql - 被 SQL Server 中的一个查询困住

sql - 获取Django模型在.save()上具有(或将调用)的SQL

sql - 您如何从 Redshift 将数据卸载到 S3 并在文件名中包含日期

transactions - 智能合约和交易有什么区别?

php - 给定多个连接时,Mysql 事务的原子性如何?