以下两种情况对应mysql默认隔离级别:
1,
时间 | 事务A | 事务B |
开启事务 begin; |
||
开启事务 | ||
mysql>select balance from account where userid=1; +---------+ |
||
select balance from account where userid=1; +---------+ |
||
mysql>update account set balance=200.00 where userid=1; | ||
提交事务 commit; |
||
mysql>update account set balance=400.00 where userid=1; | ||
提交事务 commit; |
||
select balance from account where userid=1; +---------+| balance | +---------+ | 400.00 | +---------+ |
select balance from account where userid=1; +---------+| balance | +---------+ | 400.00 | +---------+ |
2,
时间 | 事务A | 事务B |
开启事务 begin; |
||
开启事务 | ||
mysql>select balance from account where userid=1; +---------+ |
||
select balance from account where userid=1; +---------+ |
||
mysql>update account set balance=(balance+100.00) where userid=1; 注:这时其他事务改不了balance字段,事务会等待。 |
||
提交事务 commit; |
||
mysql>update account set balance=(balance+300.00) where userid=1; | ||
提交事务 commit; |
||
select balance from account where userid=1; +---------+| balance | +---------+ | 500.00 | +---------+ |
select balance from account where userid=1; +---------+| balance | +---------+ | 500.00 | +---------+ |