I have written a simple web service (asmx) in c#
The clients hit the webservice with post and a title parameter and expect the service to return an object with more details about that title key.
The problem that I am facing is that in the begining of the execution of my webservice I check to see if that title provided to me, exists in my DB. If not I construct the correct object I save it to my DB to have it for future searches in order to avoid reconstructing it (because I use third-party webservices for the construction) and then I send back in json that object that the client asked for.
This however leads to a racing condition because 2 clients may ask for the same title and end up inserting to the database the same object twice because one of them was not fast enough to write the object before the other one asks for it.
I use Mysql database and I am trying to figure out how to avoid this racing condition?
Is the key a stored procedure?
How can I solve this?
Note: The key in my DB is the title column and the year column.
Thanks in advance
You could add a SQL Server UNIQUE constraint on the column to force uniqueness. If the race condition occurs, you can handle the error (from the constraint) in your service code by catching it and simply querying for the title again.