Skip to content

Instantly share code, notes, and snippets.

@barinek
Created March 31, 2010 21:04
Show Gist options
  • Save barinek/350898 to your computer and use it in GitHub Desktop.
Save barinek/350898 to your computer and use it in GitHub Desktop.
ara's isolation
a.rb
require 'rubygems'
require 'mysql'
loop do
# Datum.transaction do
connection = Mysql.new('localhost', 'root', 'password', 'ara_example')
connection.autocommit false
statement = connection.prepare("select value from data where id = 1")
statement.execute
a_value = statement.fetch.to_s
statement = connection.prepare("select value from data where id = 2")
statement.execute
b_value = statement.fetch.to_s
if a_value > b_value
puts "a.value=0 / b.value=1"
# Datum.a.update_attributes :value => 0
# Datum.b.update_attributes :value => 1
statement = connection.prepare("update data set value = 0 where id = 1")
statement.execute
statement = connection.prepare("update data set value = 1 where id = 2")
statement.execute
else
puts "a.value=1 / b.value=0"
# Datum.a.update_attributes :value => 1
# Datum.b.update_attributes :value => 0
statement = connection.prepare("update data set value = 1 where id = 1")
statement.execute
statement = connection.prepare("update data set value = 0 where id = 2")
statement.execute
end
connection.commit
connection.close
# end
end
b.rb
require 'rubygems'
require 'mysql'
loop do
# Datum.transaction do
connection = Mysql.new('localhost', 'root', 'password', 'ara_example')
connection.autocommit false
statement = connection.prepare("select value from data where id = 1")
statement.execute
a_value = statement.fetch.to_s
statement = connection.prepare("select value from data where id = 2")
statement.execute
b_value = statement.fetch.to_s
if a_value > b_value
puts "a.value=-1 / b.value=0"
# Datum.a.update_attributes :value => -1
# Datum.b.update_attributes :value => 0
statement = connection.prepare("update data set value = -1 where id = 1")
statement.execute
statement = connection.prepare("update data set value = 0 where id = 2")
statement.execute
else
puts "a.value=0 / b.value=-1"
# Datum.a.update_attributes :value => 0
# Datum.b.update_attributes :value => -1
statement = connection.prepare("update data set value = 0 where id = 1")
statement.execute
statement = connection.prepare("update data set value = -1 where id = 2")
statement.execute
end
connection.commit
connection.close
# end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment