Clear ORM
Search…
Locks

Model lock

You can use lock using with_lock method on query collection:
1
Clear::SQL.transaction do
2
# SELECT * FROM users WHERE organization = 'Crystal Lang' FOR UPDATE
3
User.where(organization: "Crystal Lang").with_lock.each do |user|
4
# Do something with your users
5
end
6
end
Copied!
with_lock offers optional parameters (default: "FOR UPDATE"), to setup the lock options you want (ex: with_lock("FOR UPDATE SKIP LOCKED"))
Lock work only inside transaction. Without transaction block, the call might fail.
Last modified 1yr ago
Copy link