How to perform SELECT FOR UPDATE with SKIP LOCKED on MySQL with Django

时间:2018-03-09 19:01:20

标签: mysql django pessimistic-locking

I have a Django project which uses a MySQL v5.5 backend with InnoDB storage.
To avoid race condition updates in DB, I'm using select_for_update to lock the rows. Now if this lock stays for a long time, any queries on the locked rows will timeout.
I want to avoid this with one of the following options:

  1. Skip the rows which are locked, similar to SKIP LOCKED option.
  2. Return immediately if some rows you're querying are locked, similar to NOWAIT option.
  3. Reduce the lock wait timeout period for specific queries.

How do I perform any of these with Django ORM?

1 个答案:

答案 0 :(得分:1)

来自Django docs

  

您还可以使用select_for_update(skip_locked = True)忽略锁定的行