Concurrent requests

Hi all,

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.

Thank you in advance!

If you have a uniqueness validation then you should always also enforce that in the database, for exactly the reason you describe. See

Colin

Hey!

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