unknown wrote:
> #console 1
>> r = Review.new(:member_id => 1, :business_id => 4)
This line does absolutely nothing at the database level. All you have 
done here is make a new Ruby object.

>> r.sleep_for = 5
>> r.save!
This row create a database transaction, writes the data from the Ruby 
object's attributes and ends the transaction.

> => true
> 
> #console 2 (during the 5 sec sleep)
>> r = Review.new(:member_id => 1, :business_id => 4)
Again the database know nothing about the creation of the Ruby object.

>> r.save!
Since this executes before you have saved the first record, and 
validations for both records have already occurred your uniqueness 
validation WILL fail and allow a duplicate to be recorded in the 
database.

> => true
> 
> I would expect the first save to lock the table and the second to fail
> after 5 seconds.  Instead they both succeed and I end up with bad data
> in the db.  It also fails if I wrap both save! calls in
> Review.transaction blocks.
Locking the table in this scenario would be death to scalability. What 
most people do in this case is to use the validates_uniqueness_of for 
convenience, but also ensure data integrity by adding the proper unique 
index to the database.

A fail-and-recover scheme is what is needed here. As much as table 
locking sounds like the way to go, it just isn't because it's way to 
expensive.

I also understand your issue with anonymous users (especially since you 
yourself have chosen to be an anonymous coward on this forum -- kidding 
a little there hehe). But, that's just something you'll have to weigh 
for yourself.

As Frederick has already mentioned: race conditions are a tough problem 
to solve and are best done at the database layer. They are a lot more 
difficult to solve at the model object layer, which is the layer where 
validations are performed). This, however, does not render validations 
worthless. AFAIK uniqueness validations are the only ones that suffer 
from this issue.
-- 
Posted via http://www.ruby-forum.com/.

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