Hopefully the answer for other questions apart from consistent error reproduction steps were answered. Here is how
I was able to reproduce the exact deadlock error consistently'System.Data.SqlClient.SqlException (0x80131904): Transaction (Process ID 90) was deadlocked on lock | communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction. Error Number:1205,State:78,Class:13 '
I created a test table
CREATE TABLE TestTable( Name NVARCHAR(MAX) )
and added Ids to the table
ALTER TABLE TestTable ADD [Id] [bigint] IDENTITY(1,1) NOT NULL
and inserted 10 lakh entries
DECLARE @Counter BIGINT = 0 WHILE(@COunter< 1000000) BEGIN INSERT INTO TestTable VALUES('Value'+Cast(@Counter AS NVARCHAR(MAX))) SET @Counter = @Counter + 1 END
and have created a stored procedure which will lock the rows and update the value in the column for the Ids between @StartValue and @EndValue
CREATE OR ALTER PROC UpdateTestTable ( @StartValue BIGINT, @EndValue BIGINT ) AS BEGIN UPDATE TestTable WITH(ROWLOCK) SET Name = 'Value'+CASt(@StartValue+Id+DATEPART(SECOND, SYSDATETIMEOFFSET()) AS NVARCHAR(MAX)) WHERE Id BETWEEN @StartValue AND @EndValue END GO
I called this stored procedure from the code with @StartValue and @EndValue set to 1 to 1000, 1001 to 2000, .... so on up to 10 lakh parallely for each range and I was able to reproduce the error consistently.