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
([email protected]:3306) [test]> show variables like '%iso%';
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| tx_isolation | REPEATABLE-READ |
+---------------+-----------------+

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

([email protected]:3306) [test]> begin;
Query OK, 0 rows affected (0.00 sec)
([email protected]:3306) [test]> select * from t;
+----+------+
| id | c |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
+----+------+
4 rows in set (0.00 sec)
                                      session2: ([email protected]:3306) [test]> update t set c=id+1;
([email protected]: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的正确的值
([email protected]:3306) [test]> select * from t;
+----+------+
| id | c |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
+----+------+
4 rows in set (0.00 sec)
([email protected]:3306) [test]> commit;
Query OK, 0 rows affected (0.00 sec)

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

猜你喜欢

转载自www.cnblogs.com/yhq1314/p/10043143.html