MySQL 更新失效

create table t(id int not null PRIMARY key,c int default null) engine=innodb;
insert into t(id,c)values(1,1),(2,2),(3,3),(4,4);

session1                                    session2
(system@127.0.0.1:3306) [test]> show variables like '%iso%';
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| tx_isolation | REPEATABLE-READ |
+---------------+-----------------+

(system@127.0.0.1:3306) [test]> show variables like 'auto%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
| autocommit | ON |
| automatic_sp_privileges | ON |
+--------------------------+-------+

(system@127.0.0.1:3306) [test]> begin;
Query OK, 0 rows affected (0.00 sec)
(system@127.0.0.1:3306) [test]> select * from t;
+----+------+
| id | c |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
+----+------+
4 rows in set (0.00 sec)
                                      session2: (system@127.0.0.1:3306) [test]> update t set c=id+1;
(system@127.0.0.1:3306) [test]> update t set c=0 where id=c;
Query OK, 0 rows affected (4.39 sec)
Rows matched: 0 Changed: 0 Warnings: 0
//session1更新失效,update是当前读,此时已经找不到id=c的值,所以更新失败
//rc情况,update还是会更新失效,跟隔离级别无关,都是当前读,只不过rc情况,下面的select就能读到session2的正确的值
(system@127.0.0.1:3306) [test]> select * from t;
+----+------+
| id | c |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
+----+------+
4 rows in set (0.00 sec)
(system@127.0.0.1:3306) [test]> commit;
Query OK, 0 rows affected (0.00 sec)

(system@127.0.0.1:3306) [test]> select * from t;
+----+------+
| id | c |
+----+------+
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
| 4 | 5 |
+----+------+
4 rows in set (0.00 sec)

posted @ 2018-11-30 11:22  春困秋乏夏打盹  阅读(872)  评论(0编辑  收藏  举报