I am running into ‘concurrent POST requests’ issue in the application
that
I would like to handle.
It’s resulting in duplicate entries in database for columns with
indexes.
I am thinking of adding unique constraint to the table columns (with
index) and handle resulting database errors in the application.
Another way is to lock the row using something like
order.transaction do
order.lock!*
end
end
I would like to know your thoughts about the 2 approaches.
It will be great if you could direct me to useful links.
I am running into ‘concurrent POST requests’ issue in the application that I
would like to handle.
It’s resulting in duplicate entries in database for columns with indexes.
I am thinking of adding unique constraint to the table columns (with index)
and handle resulting database errors in the application.
If you have a uniqueness validation then you should always also
enforce that in the database, for exactly the reason you describe.
See
I think it’d be helpful if you provide us with a little bit of context.
What are you trying to achieve?
Basically, you have two lines of defense: the app and the database. Like
Colin said, if uniqueness is an essential constraint in your domain then
you should enforce it at the database level. You can also use Active
Record
uniquness validator to display a nice error message (but this w/o the
database-level constraint is not enough). If there’s a race condition
Active Record will raise RecordNotUnique.
If you tell us a bit more we might be able to give you more specific
advice.
Best regards
Greg Navis
I help small tech companies to scale Heroku-hosted Rails apps.
Free, biweekly scalability newsletter for SaaS CEOs http://www.gregnavis.com/newsletter/
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.