6

PostgreSQL 的 pg_try_advisory_xact_lock 正确使用姿势是什么

 2 years ago
source link: https://www.v2ex.com/t/787443
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.

V2EX  ›  PostgreSQL

PostgreSQL 的 pg_try_advisory_xact_lock 正确使用姿势是什么

  dzdh · 5 小时 10 分钟前 · 139 次点击

看到 pg_try_advisory_xact_lock (咨询锁)可以事务中免等待

实际场景是啥?

看很多描述是秒杀场景,那就拿秒杀来说,,一件商品,多人下单,那就是多个事物,只有一个人能下单(扣减库存)成功。

然后没拿到锁的呢?直接前端抛出个抢购失败的异常?然后刷新页面一看还有库存?业务代码逻辑自动重试?重试多少次呢?

因为很多场景其实并不都是极限秒杀场景(成百上千人抢),可能就是平常的一个商品,某个店铺搞了个活动(平台也不晓得)突然就大流量上来了。

就是不固定不定时毫无预兆的普通商品抢购,自动重试次数少了,刷新页面看还有库存。自动重试次数多了,那还不如事物里锁这条数据呢。

更具体的使用场景或姿势是啥?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK