On Wed, Sep 10, 2008 at 11:59 AM, Difei Z.
[email protected] wrote:
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!
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