http://api.rubyonrails.org/classes/ActiveRecord/Locking/Pessimistic.html
On May 4, 7:27 am, Amit Rawal
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> Hello,
>
> Here is the logic that I am trying to achieve. I have a User Model and
> the business rule is that user can accept maximum 5 friends requests.
>
> for this I have an accept method in User Model which looks similar to
> below code snippet
>
> def accept(friend)
> raise ArgumentError if self.friends.count >= MAX_FRIENDS_ALLOWED
> ...
> ...
> #code to add friend to friends list
> end
> As you can see that I basically query for count of friends for a user
> and if the count is greater then or equal to MAX_FRIENDS_ALLOWED the
> friend accept request should not go through.
>
> From the UI perspective I have a link_to_remote, "Accept" link
that
> calls an action which calls the User Model''s "accept"
method. Basically
> UI looks like
>
> FRIEND NAME | Action
> ---------------------------
> AAAAA | Accept
> BBBBB | Accept
> CCCCC | Accept
> DDDDD | Accept
> EEEEE | Accept
> FFFFF | Accept
>
> Now from UI I try to accept multiple friends requests by clicking on
> "Accept" link (which makes an ajax request) quickly.
>
> When I run this in under normal development environment (running single
> mongrel), everything works fine i.e. the validation put in User
Model''s
> "accept" method works.
>
> After that I configured 4 mongrel in a cluster and ran it behind
> lighttpd and then ran in to trouble.
>
> I guess the problem here is that since I run cluster of mongrels each
> mongrel executes the request in separate process because of which before
> the a friend is accepted, another process executes
"self.friends.count"
> and it will not reflect the updated count. I tried to enclose the above
> statements in Activerecord transaction, but it did not work and the
> reason I think is that transaction is per-database connection and each
> process will spawn a separate connection.
>
> Please suggest any workarounds or probably solution to this issue.
> --
> Posted viahttp://www.ruby-forum.com/.