On Wed, Sep 10, 2008 at 11:59 AM, Difei Zhao
<[EMAIL PROTECTED]> wrote:
>
> Greetings all,
>
>  I have a model which holds a counter field, the field have to be
> incremented frequently to record the access frequency of the specific
> object:
>
> @obj = Temp.find(id)
> @obj.counter += 1
> @obj.save
>
>  The implementation seems quite simple, but I am worrying about the
> race condition (if there are any, I do not know whether rails is
> thread-safe) since these statements may be interleaved. Could anyone
> help about this? Thanks very much!
>
> Difei

You're correct to worry about a race condition, but for the wrong
reason. Rails deployments up to now and for a while yet into the
future are clusters of mongrels / thins / etc, and as such nothing is
threaded. The race condition in who (as in, which Mongrel) gets to
save to the database first. As for protecting against this, you'll
need to make sure to lock the row you working with, update the
counter, save, then unlock. You'll want to do this as quick as
possible of course, but it still could cause some slowdown if a lot of
processes are trying to do the same thing.

If there are better ways around this, I'd definitely be interested in
hearing them.

Jason

--~--~---------~--~----~------------~-------~--~----~
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