Last active
August 25, 2016 17:34
-
-
Save amit/b9553715e01188e48b78616ca66c12b8 to your computer and use it in GitHub Desktop.
Activerecord test of simulteneous access serialization
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
threads = [] | |
(0..4).each do | |
threads << Thread.new { | |
row = XAuth::Variable.where(name: "current_member_id").first_or_create | |
row.with_lock do | |
sleep(2) | |
oldvalue = row.value.to_i | |
newvalue = oldvalue + 1 | |
row.value = newvalue | |
row.save! | |
puts "#{Thread.current.object_id} ==> Old: #{oldvalue} , New val = #{newvalue}" | |
end | |
} | |
end | |
threads.each { |thr| thr.join } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment