Created
June 4, 2010 05:27
-
-
Save euge/424996 to your computer and use it in GitHub Desktop.
mysql locking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=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