Rupal,
Deadlocks will be consistently generated across multiple sessions if the conditions for deadlocks are met.
A deadlock only occurs if there is a collision in the locks from two separate sessions where the lock waits would never be resolved :
Process A aquires an update lock on Resource 1 and Process B aquires an update lock on Resource 2. Process A then attempts to aquire a lock on Resource 2, which in normal cases would wait until Process B releases the lock first. If Process B now attempts to aquire a locck on Resource 1, it means that both processes will wait indefinitely for each other to release their respective locks. This is a seen as a deadlock, and the server will have to select one of these 2 processes to be the victim. The victim's SQL batch gets terminated and all processing that it has done is rolled back.
In your example, Session 3 does not create a collision, it is simply waiting for Session 1 to release the lock(s) it has acquired on table pd1. This is a simple 'lock wait', and should be indicated as a blocking lock if you do an 'sp_who' from another session - status = 'lock sleep' and a non-zero blk_spid, indicating which spid is blocking it.
In my test scenario Session 1 kept on being the victim, and not Session 2, as you would logically expect. This is likely due to the number of records involved and CPU usage being higher for session 2. Deadlock victims are supposed to be selected based on the number of CPU cycles accumulated - the one with the least CPU usage gets selected as the victim. I just added a bit more processing for session 1 to get around this.
This would do the trick and indicate correct deadlock handling as expected :
Code:
--Step 1: Session 1
begin transaction
go
update pd1 set type='P' where number=0
-- Generate some extra cpu cycles to ensure that Session 1 is NOT the victim
select count(*) from pd1
select * from pd1
go
--Step 2: Session 2
begin transaction
go
update pd2 set type='P' where number=0
go
--Step 3: Session 3
begin transaction
go
update pd3 set type='P' where number=0
go
--Step 4: Session 1
select * from pd2
select * from pd3
go
--Step 5: Session2
select * from pd1
go
--Throws a deadlock error.
--Step 6: Session3
select * from pd1
go
--Also throws a deadlock error.