I read on google that deadlock can be managed by using UPDLOCK table hint. But the below scenario does not avoid deadlock:-
-----session 1------------
set transaction isolation level repeatable read
begin transaction
select * from c with (updlock)
-----session 2------------
set transaction isolation level repeatable read
declare @var int = 1
begin transaction
select * from c
Now when we go back to session 1 and issue a update query we will get deadlock in session 2. How did it avoid deadlock then and there is no use of updlock. We should use tablockX hint instead which does not allow any type of locks.
-----session 1------------
update c
set name = 'qwert'
where id = 1
I found another strange observation. If i change the isolation level in the first session to serializable then updlock works and avoids deadlock.So my understanding is -
- Updlock will avoid deadlocks only when serializable isolation level is selected.
- When isolation levels other than serializable is used then shared locks from other sessions are allowed which results in deadlock.We should use tablockx in such cases.
- When serializable isolation is used then shared locks from other sessions are not allowed which helps in avoiding deadlocks.
Can someone help me here I am confused badly