I have an application lock for pessimistic concurrency and generate code in Asp.Net Mvc.
using (var scope = new TransactionScope())
{
using (var context = new Context())
{
var submitedEntity = context.Entities.Add(entity);
context.SaveChanges();
context.Entry(submitedEntity).Reload();
code = submitedEntity.Code;
scope.Complete();
}
}
And generate code in SQL Server Trigger
Create Trigger [dbo].[OnInsertEntity]
On [dbo].[TableName]
After Insert
As
Begin
Begin Transaction
Declare @ID BigInt,
@LastCode Int,
@Length Int,
@Code nChar(5)
Select @ID = ID
From Inserted
Select @LastCode = Max(Convert(Int, Code))
From [TableName]
With (TABLOCKX, HOLDLOCK)
If @@RowCount = 0
Set @LastCode = 0
Waitfor Delay '00:00:05'
Select @Length = Max_Length
From Sys.Columns
Where Name = N'Code' And
[Object_ID] = Object_ID('[TableName]')
Select @LastCode = @LastCode + 1,
@Code = Replicate('0', @Length - Len(@LastCode)) + Convert(nChar, @LastCode)
Update [TableName]
Set Code = @Code
Where ID = @ID
Commit Transaction
End
Error at:
catch (DbUpdateException ex)
Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
What is the problem?
Thanks.