Part of my application updates the table according to business logic after opening a connection at the transaction isolation level REPEATABLE READ. In a rare scenario, if this operation matches another part of the application, which opens another connection and tries to reset the same entry by default. I get the following error
Msg 1205, Level 13, State 45, Line 7 Transaction (Process ID 60) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
I think I can repeat the problem using the following example.
one.
create table Accounts ( id int identity(1,1), Name varchar(50), Amount decimal )
2.
insert into Accounts (Name,Amount) values ('ABC',5000) insert into Accounts (Name,Amount) values ('WXY',4000) insert into Accounts (Name,Amount) values ('XYZ',4500)
3.
Run a long transaction with isolation level as REPEATABLE READ
Set transaction isolation level REPEATABLE READ begin tran declare @var int select @var=amount from Accounts where id=1 waitfor delay '0:0:10' if @var > 4000 update accounts set amount = amount -100; Commit
4.
While step 3 above is still performed. Run another transaction in a different connection
Begin tran update accounts set Amount = 5000 where id = 1 commit tran
The transaction that you started in step 3 will eventually complete, but the one that you started in step 4 will fail with the following error message.
Msg 1205, Level 13, State 45, Line 7 Transaction (Process ID 60) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
What are my options that ultimately allow me to complete the transaction in step 4. The idea is to be able to reset the default value, in which case all other operations should be redefined. I do not see any problems if both transactions are not parallel.