在RR或RC隔离级别下的使用锁实现不可重复读
使用锁时是当前读(可以读取事务提交的数据), 不使用锁是快照读
mysql> select * from item;
+-----+------+----------+
| id | name | order_id |
+-----+------+----------+
| 5 | | 5 |
| 6 | | 5 |
| 9 | 23 | 4 |
| 10 | 23 | 4 |
| 11 | 23 | 5 |
| 12 | 23 | 5 |
| 13 | 23 | 6 |
| 14 | 23 | 6 |
| 20 | 23 | 5 |
| 21 | 23 | 5 |
| 22 | 23 | 5 |
| 26 | 23 | 5 |
| 30 | 23 | 1 |
| 31 | 31 | 31 |
| 333 | | 5 |
+-----+------+----------+
15 rows in set (0.00 sec)
mysql> select * from item for update;
+-----+------+----------+
| id | name | order_id |
+-----+------+----------+
| 2 | | 5 |
| 5 | | 5 |
| 6 | | 5 |
| 9 | 23 | 4 |
| 10 | 23 | 4 |
| 11 | 23 | 5 |
| 12 | 23 | 5 |
| 13 | 23 | 6 |
| 14 | 23 | 6 |
| 20 | 23 | 5 |
| 21 | 23 | 5 |
| 22 | 23 | 5 |
| 26 | 23 | 5 |
| 30 | 23 | 1 |
| 31 | 31 | 31 |
| 333 | | 5 |
+-----+------+----------+
16 rows in set (0.00 sec)
mysql> select * from item;
+-----+------+----------+
| id | name | order_id |
+-----+------+----------+
| 5 | | 5 |
| 6 | | 5 |
| 9 | 23 | 4 |
| 10 | 23 | 4 |
| 11 | 23 | 5 |
| 12 | 23 | 5 |
| 13 | 23 | 6 |
| 14 | 23 | 6 |
| 20 | 23 | 5 |
| 21 | 23 | 5 |
| 22 | 23 | 5 |
| 26 | 23 | 5 |
| 30 | 23 | 1 |
| 31 | 31 | 31 |
| 333 | | 5 |
+-----+------+----------+
15 rows in set (0.00 sec)