mysql

  • 判断值是否超过最大值,没有则更新,有则不操作

比如:团购中有订单支付成功时,更新团实时有效订单数。这个值不能用下面代码实现

select orderNum as currentNum from groupInfo where groupId = 'id';
boolean isOk = judge(orderNum, maxOrderNum);
if(isOk) {
    update groupInfo set orderNum = currentNum + 1 where groupId = 'id';
}

应该用

update groupInfo set orderNum = orderNum+1 where groupId = 'id' and orderNum+1 < maxNum;
if(affectedNum ==1) {
    isOk;
} else {
    rollback;
}
  • 判断数据是否是某个状态,如果是的更新,不是则不操作

不能用下面代码实现

select status from groupInfo where groupId = 'id';
if(status == 1) {
    update groupInfo set stauts = 2 where groupId ='id';
} else {
    done;
}

应该用

update groupInfo set status=2 where groupId = id and status = 1;
if(affectedNum == 1) {
    isOk;
} else {
    rollback;
}

问题

上面两种做法就是把并发控制交给数据库了,会增大数据库的压力,在应用层就控制住并发是最理想的。

相关文章:

  • 2022-12-23
  • 2021-08-30
猜你喜欢
  • 2021-07-29
  • 2021-12-16
  • 2022-01-07
  • 2021-07-14
  • 2022-12-23
相关资源
相似解决方案