I have tried using transactions and also setting the transaction isolation level to serializable but I still have the same issue.
Here is the way to reproduce my problem:
CREATE PROCEDURE dbo.TestRaceCondition
AS
DROP TABLE IF EXISTS dbo.TABLE_A;
WAITFOR DELAY '00:00:01';
SELECT 1 ID INTO dbo.TABLE_A;
GO
Copy and paste the following code multiple times (I do 20 times) in 3 different sessions
EXEC dbo.TestRaceCondition;
GO
Then try to execute the 3 sessions at the same time.
This is the error I get in at least one of the sessions:
Msg 2714, Level 16, State 6, Procedure dbo.TestRaceCondition, Line 46
There is already an object named 'TABLE_A' in the database.
Hopefully someone can help me, thank you in advance!
I have tried using transactions
CREATE PROCEDURE dbo.TestRaceCondition
AS
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
DROP TABLE IF EXISTS dbo.TABLE_A;
WAITFOR DELAY '00:00:01';
SELECT 1 ID INTO dbo.TABLE_A;
COMMIT TRAN
I expect running the procedure multiple times with no error.
It seems
DROP TABLE IF EXISTSonly applies aSch-Slock temporarily, and if the table needs to be dropped only then does it upgrade to aSch-Mlock.While there are many ways to force an
Sch-Mlock, a more idiomatic way to do this might be to usesp_getapplock, which can create an arbitrary application-defined lock.I would maintain though, that if you are dropping and re-creating the same table continuously then you are probably doing something wrong.