Hi!

Say we have a rails active-record-based model called "instance" which
can have different states - STOPPED, RUNNING, STARTING_UP,
SHUTTING_DOWN, etc...

There is a method #start which changes the instance state to
STARTING_UP only if it is STOPPED.

def start
  if self.state == STOPPED
   self.state = STARTING_UP
   self.save
   ...
  end
end

As you understand if we have multiple concurrent requests for #start,
there is a possibility of a race condition, where two or more
processes are running the if branch.

I want the #start request to be atomic, meaning that only one of the
concurrent processes can actually execute the if branch, others must
be waiting or get some kind of error.

Should the request be somehow wrapped into a DB transaction?

-Kirill
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to