现在我的card表里的count为8 表示有8张卡 如果我有10个并发线程去调用 最后的结果是card表count为0 但是mycard表 会有10条记录
就是10个人抢8张卡 结果10个人都抢到了
就是10个人抢8张卡 结果10个人都抢到了
declare update_count int;
declare tmp_update_count int default 0;
declare t_error integer default 0;
declare temp int;
declare continue handler for SQLEXCEPTION set t_error = 1;
start transaction;
update card set count = count-1 where count >= 1 and cardId = cardId;
select row_count() into update_count;
select sleep(1) into temp;
if update_count > 0 then
insert into myCard (UserId, ShopID, CardID, OrderID, R_Date, Score) values (userId, shopId, cardId, orderId, applyDate, score);
select row_count() into tmp_update_count;
end if;
if t_error = 1 then
set tmp_update_count = 0;
rollback;
else
commit;
end if
select tmp_update_count as update_count;
declare tmp_update_count int default 0;
declare t_error integer default 0;
declare temp int;
declare continue handler for SQLEXCEPTION set t_error = 1;
start transaction;
update card set count = count-1 where count >= 1 and cardId = cardId;
select row_count() into update_count;
select sleep(1) into temp;
if update_count > 0 then
insert into myCard (UserId, ShopID, CardID, OrderID, R_Date, Score) values (userId, shopId, cardId, orderId, applyDate, score);
select row_count() into tmp_update_count;
end if;
if t_error = 1 then
set tmp_update_count = 0;
rollback;
else
commit;
end if
select tmp_update_count as update_count;