mysql默认事务隔离级别-事务并发实战

时间:2021-03-01 17:57:02

以下两种情况对应mysql默认隔离级别:

1,

时间 事务A 事务B
   

开启事务

begin;

  开启事务  
   

mysql>select balance from account where userid=1;

+---------+
| balance |
+---------+
| 100.00  |
+---------+

 

select balance from account where userid=1;

+---------+
| balance |
+---------+
| 100.00  |
+---------+

 
    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;

+---------+
| balance |
+---------+
| 100.00  |
+---------+

 

select balance from account where userid=1;

+---------+
| balance |
+---------+
| 100.00  |
+---------+

 
   

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  |

+---------+