Skip to content

Instantly share code, notes, and snippets.

@xaviershay
Forked from benhoskings/atomic.rb
Created February 22, 2010 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xaviershay/310659 to your computer and use it in GitHub Desktop.
Save xaviershay/310659 to your computer and use it in GitHub Desktop.
module ActiveRecord
class Base
def atomic &block
transaction {
lock!
yield
}
end
end
end
class Sequence < ActiveRecord::Base
# so instead of this,
def nextval
transaction {
lock!
returning self.value += 1 do
save!
end
}
end
# we can do this
def nextval
atomic {
returning self.value += 1 do
save!
end
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment