Skip to content

Instantly share code, notes, and snippets.

@amit
Last active August 25, 2016 17:34
Show Gist options
  • Save amit/b9553715e01188e48b78616ca66c12b8 to your computer and use it in GitHub Desktop.
Save amit/b9553715e01188e48b78616ca66c12b8 to your computer and use it in GitHub Desktop.
Activerecord test of simulteneous access serialization
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