mysql - 来自 Django select_for_update 的锁定记录是否受更新值的影响

标签 mysql django concurrency mysql-python isolation-level

我知道如果我们有多个并发用户, select_for_update 会锁定正在查询的记录。但它是否也会影响对查询记录的评估。

例如,如果用户 A 和用户 B 基于一个 bool 变量 'Available' 同时查询表 'Jobs'。
两个用户都希望访问可用作业的记录(其中可用 = True)。

用户 A 设法首先排队,他正在访问的记录现在已为用户 B 锁定。

如果用户 A 将所有记录更改为 False,那么用户 B 是否没有任何记录?或者他是否获得与用户 A 相同的记录,但可用性等于 False?

另外,select_for_update() 是否适用于 MySQL?或者它对并发查询有不同的解决方案吗?

最佳答案

来自关于 select_for_update 的文档

Usually, if another transaction has already acquired a lock on one of the selected rows, the query will block until the lock is released. If this is not the behavior you want, call select_for_update(nowait=True). This will make the call non-blocking. If a conflicting lock is already acquired by another transaction, DatabaseError will be raised when the queryset is evaluated. You can also ignore locked rows by using select_for_update(skip_locked=True) instead. The nowait and skip_locked are mutually exclusive and attempts to call select_for_update() with both options enabled will result in a ValueError.



它基本上意味着当时只有一个查询通过,当它被处理时,另一个查询会轮到(互斥行为)

在你的例子中考虑例如
...filter(available=true).select_for_update()

如果第一个更改记录,其他请求将获得新状态(没有记录首先设置为 false ),因为该特定线程等待对数据库进行查询

关于mysql - 来自 Django select_for_update 的锁定记录是否受更新值的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61776201/

相关文章:

mysql - 在 MySQL 的父子模型中计算深度

python - 如何通过数据库查询提高 python 代码的时间

Django Admin - 为特定模型管理员插入自定义 HTML 代码?

java - 如何检查 ConcurrentLinkedQueue 的 size() 或 isEmpty()

java - 只读同步

.net - Reactive Extensions 中的 BufferWithTime 会重叠调用 OnNext 吗?

python - Python 中的 C func 中的浮点变量格式不正确

PHP/MySQL 从列表中删除重复名称

php - 取不同值Mysql与PHP相乘

python - IOError : request data read error