Skip to content

Instantly share code, notes, and snippets.

@ilake
Last active March 9, 2016 06:57
Show Gist options
  • Save ilake/f163ccf349fdc288e009 to your computer and use it in GitHub Desktop.
Save ilake/f163ccf349fdc288e009 to your computer and use it in GitHub Desktop.
# When you use select * for update, other process only could touch the query when it be `committed`
# Query 1
User.transaction do
User.find(1).groups.joins(:owner).lock.each(&:destroy)
end
User.transaction do
# If the groups has same owner, it will wait the query 1
# If not, it will continue
User.find(2).groups.joins(:owner).lock.each(&:destroy)
end
# -----------------
User.transaction do
# if the query could not find any record, it will lock whole table
User.where(credits: 1005).lock.first
sleep 60
end
# It will hang over there
User.find(1653).update_columns(credits: 1003)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment