更新Where(选择计数(*))原理和种族条件。建议?

发布于 2025-02-11 18:39:39 字数 1399 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从来不烧饼 2025-02-18 18:39:39

外,没有办法这样做。

  1. 使用序列化交易隔离

  2. 锁定所有视线以序列化操作

不清楚您到底要做什么,但是对时间戳范围的排除限制也许会有所帮助。

There is no way to do this except

  1. using SERIALIZABLE transaction isolation

  2. locking everything in sight to serialize operations

It is unclear what exactly you are trying to do, but perhaps an exclusion constraint on timestamp ranges can help.

意犹 2025-02-18 18:39:39

通常,防止其他查询以锁定目的访问行的方法是使用选择更新。我不确定您是否正在使用PostgreSQL或SQLITE,但是您可以阅读有关PostgreSQL功能在这里。 Sqlite不支持它。

这个想法是,您可以锁定感兴趣的行,然后执行所需的任何操作,而不必担心其他查询更新该行,然后进行交易。

一个常见的情况是,当您试图预订预订时,您的示例似乎正在按照这些方式做一些事情。我们将在包含要预订的资源的行上进行选择更新,然后检查用户想要预订的可用日期,并且一旦我们确保可用于该资源的日期可用,继续预订。 选择更新可防止其他人试图同时预订相同资源的可能性。

In general, the way to prevent other queries from having access to a row(s) for locking purposes is to use SELECT FOR UPDATE. I'm not sure if you're using postgresql or sqlite, but you can read about the postgresql functionality here. Sqlite does not support it.

The idea is that you can lock the row you for which are interested, and then do whatever operations you need to without worrying about other queries updating that row, and then commit your transaction.

A common scenario for this would be when you're trying to book a reservation, as it looks like your example may be doing something along those lines. We would do a SELECT FOR UPDATE on the row containing the resource we want to book, then check the available dates the user is wanting to book, and once we have ensured that the dates are available for that resource, go ahead and book it. The SELECT FOR UPDATE prevents the possibility of other people trying to book the same resource at the same time we are.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文