- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
function-6ren">
我在一个大型项目中使用 django。
我从模型的 save
内部调用 celery task
,该模型调用一个方法,该方法在循环中调用另一个方法。即:
celery task --> function A()
A() --> for i in range(1,100): call function B()
现在 B()
被一个 atomic()
装饰器包裹,并且在其中有一个 select_for_update
调用。
我仍然得到 TransactionManagementError('select_for_update cannot be used outside a transaction.',)
我不知道这是为什么。我已经通过将任务延迟几秒钟进行了测试,以便在调用任务时提交 save
。没有帮助。
我的问题是:当我已经在原子 block 中时,为什么会收到 TransactionManagementError
?
最佳答案
@ketanbhatt 这可能有帮助
https://docs.djangoproject.com/en/1.9/ref/models/querysets/#select-for-update
Evaluating a queryset with select_for_update() in autocommit mode on backends which support SELECT ... FOR UPDATE is a TransactionManagementError error because the rows are not locked in that case. If allowed, this would facilitate data corruption and could easily be caused by calling code that expects to be run in a transaction outside of one.
https://docs.djangoproject.com/en/1.9/topics/db/transactions/#managing-autocommit
Django will refuse to turn autocommit off when an atomic() block is active, because that would break atomicity.
关于django - "select_for_update"从原子 block 调用仍然是 TransactionManagementError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36299291/
我想创建一个模型,其 ID 等于该模型当前最大 ID 加一(如自动增量)。我正在考虑使用 select_for_update 执行此操作,以确保当前最大 ID 不存在竞争条件,如下所示: with t
我用过 select_for_update()很多。但是,在我使用过的所有情况下,它都是通过这样的经理来完成的: with transaction.atomic(): transaction
我正在做一个类似于电子商务的项目,在这个项目中,我有一些模型来表示存储中有一定数量的产品,用户可以购买一定数量的产品,只要它不超过存储的数量.我想避免在服务器收到多个购买同一产品的请求时发生竞争条件。
Django documentation指出: If you were relying on “automatic transactions” to provide locking between s
select_for_update 的 Django 文档说 Using select_for_update() on backends which do not support SELECT ...
我有一个代码,应该可以在并发请求和高负载下工作。 我写了一个例子来更好地理解我正在尝试做的事情: def add_tag(): with transaction.atomic():
我有一行整数值,该行有很多并发请求,我希望每次读取操作后都进行更新操作(增量),并保持数据一致,只能一个请求读取->同时更新。 我做了一些研究并弄清楚了 select_for_update(),我还想
我有一些代码,如果嵌套调用 select_for_update() 会简单很多,但恐怕我自己陷入了僵局。 例子: with transaction.atomic(): uu1 = UniUse
我知道如果我们有多个并发用户, select_for_update 会锁定正在查询的记录。但它是否也会影响对查询记录的评估。 例如,如果用户 A 和用户 B 基于一个 bool 变量 'Availab
我有一个由 MySQL 数据库支持的 Django 应用程序。我最近将一段代码从请求流中移出并放入了一个流程中。该代码使用 select_for_update() 锁定数据库中受影响的行,但现在我偶尔
代码崩溃 @transaction.atomic def cancel_one(o_id): order= Order.objects.select_for_update().get(id=o
如果我执行 select_for_update,该锁何时/如何释放?一些示例代码: for rec_id in list(1,2,3): record = MyModel.objects.se
有谁知道您是否可以在单个查询中同时执行 .select_for_update() 和 .select_related() 语句?如: employee = get_object_or_404(Empl
我在一个大型项目中使用 django。 我从模型的 save 内部调用 celery task,该模型调用一个方法,该方法在循环中调用另一个方法。即: celery task --> function
Django documentation州 If you were relying on “automatic transactions” to provide locking between sel
正如 Django 文档所说,select_for_update返回一个 Queryset。但是 get 没有。现在我有一个查询,我确信它只会返回一个元组。但我还需要为此事务获取锁。所以我正在做类似的
我遇到了一个奇怪的情况,当在 transaction.atomic() block 中使用 select_for_update() 时,Django 和 Postgres 中记录的查询顺序不同。 基本
我有类似的东西: newsletter = Newsletter.select_for_update().latest() newsletter.started_sending = timezone.
我有一个如下表: SELECT id, name FROM node; +----+------+ | id | name | +----+------+ | 5 | na | +----+--
我是一名优秀的程序员,十分优秀!