Skip to content

Instantly share code, notes, and snippets.

@euge
Created June 4, 2010 05:27
Show Gist options
  • Save euge/424996 to your computer and use it in GitHub Desktop.
Save euge/424996 to your computer and use it in GitHub Desktop.
mysql locking
=begin
FOR UPDATE
- no one else can SELECT ... FOR UPDATE the row
- no one else can update/delete the row
LOCK IN SHARE MODE
- others may SELECT ... LOCK IN SHARE MODE
- no one else can update/delete the row
=end
require 'rubygems'
gem 'activerecord', '=2.3.5'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "root",
:database => "syslitics_development"
)
class Account < ActiveRecord::Base
end
Account.transaction do
a = Account.find(71, :lock => true)
puts a.inspect
sleep 10000
end
# this will block
#puts Account.find(71, :lock => true).inspect
# but this won't
#puts Account.find(71).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment