MySQL事务&并发实际操合集

1. "UPDATE tbl SET count = count - 10 WHERE id = 1"这种SQL能否保证并发事务安全。

答:可以,UPDATE操作会针对id加锁,且"x = x - y"会再最新提交的结果上计算。

演示:

t1

set autocommit = 0;
begin;
update clockin_test set count = count - 10;

t2

set autocommit = 0;
begin;
update clockin_test set count = count - 5;

然后会发现t2卡住了,即被锁了。如果你有查询锁权限,应该可以看到锁

然后提交t1,再提交t2,最后结果是正确的。

2.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *