Does the Rails `with_lock` block read?

Suppose I have an Article that I want to make some changes.

If I use with_lock , will it block other processes from reading this row in the Articles table?

eg.

  @article = Article.find(1) Article.with_lock #do something end # In another process @article = Article.find(1) # will this lookup be blocked by the first process? 
+6
source share
1 answer

Per documentation , by default it has SELECT ... FOR UPDATE .

A SELECT ... FOR UPDATE in a row does not block other row reads. It only blocks other sessions from getting a FOR UPDATE (write) or FOR SHARE (read) lock on a row. The string remains normally readable for sessions that perform SELECT without any FOR UPDATE or FOR SHARE .

So, in this case you will need to find line, then do lock('FOR SHARE') if you want to make sure that someone else does not have a FOR UPDATE lock.

For more information, see the PostgreSQL documentation on explicit locking .

+12
source

Source: https://habr.com/ru/post/975688/


All Articles