I have an issue that the same stored procedure is being invoked at exactly the same time with exactly the same paramenters.
The purpose of the stored procedure is to fetch a record if it exists or to create and fetch the record if it does not exist.
The problem is that both threads are checking the existence of the record and reporting false and then both are inserting a new record, creating a duplicate in the database.
I tried sticking the operations inside a transaction but this merely produced hundreds of deadlocks.
Is there any way that I can check for the existence of the record in a thread safe manner so that the second thread won't do its read until the first has finished its insert? I have no control over the threads themselves, only over the stored procs they are executing.
Any help would be appreciated,
Thanks.