应用在商城的抢购:
给定一个总数量,抢完完事.使用了事务控制不能多卖.
代码:
CREATE PROCEDURE global_shops(
 in_item_id int(10),
 in_max_count int(10),
 in_count int(10),
 OUT out_can_buy tinyint(2)
  )
BEGIN
  declare currcount int(10);
START TRANSACTION;
  update g_shop set count = count + in_count where item_id = in_item_id;
  select count into currcount from g_shop  where item_id = in_item_id;
   if( currcount <= in_max_count) then
    set out_can_buy = 1;
    COMMIT;
  else
    set out_can_buy = 0;
    ROLLBACK;
  end if;
END参数分别是:抢购的itemId  总数量  和 该次交易数量  以及 是否可交易的状态.
问题在平时没有暴漏出来. 一切都正常
 有一次机器IO非常大的时候.数据库很卡.然后show processList 发现很多进程停留在freeing items这个状态.....  
 然后这个时候.事务就失去了本来的作用.out_can_buy返回1  往往卖出多的出去.使用的是云数据库,没法换数据库  没法换机器.想先看看自己有没有办法改进.数据库mysql事务