3

脱离事务后,乐观锁怎么保证当主业务失败后所有关联操作都一定会回滚的

 2 years ago
source link: https://www.v2ex.com/t/866900
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  问与答

脱离事务后,乐观锁怎么保证当主业务失败后所有关联操作都一定会回滚的

  dzdh · 2 小时 33 分钟前 · 349 次点击

还拿经典的下单逻辑来说。

我的整个逻辑可能是这样的:

  • update goods set stock=$n where id = $ID and stock=$old_stock 更新库存
  • update coupons set used = 1, order_id = $n where id = $ID and used = 0 and order id is null
  • update credits set credits = $n where id = $ID and credits = $old_credits

假设,如果第一个 sql 成功,第二个 sql 成功(重试 5 次,间隔 1 秒),第三个失败了。然后 mysql 崩了(无论因为网络啊、进程啊、硬盘满了啊各种概率因素)导致后续的 sql 都不可能执行成功,而且崩了至少有一小时。

怎么保证在 1 小时后,刚下的订单为失败?或者让后续逻辑能继续执行?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK