fyi this is the little test I did:first I tried it with the default isolation level and without any locking hint. The head.balance was messed up because of the sequence. but no deadlocks. The first transaction overwrote the balance set from the second transaction.Next I tried "with (holdlock)" on the READ. With this option the second transaction got killed because of deadlock.Next I tried no locking hint but setting the transaction isolation level serializable. As expected, same results as "with (holdlock)"Next I tried "with (updlock)" and the second transaction was blocked for duration of the first transaction but no deadlock and the head.balance was correct.use pubs----------------------------------------------------------------SET UP the Testgocreate table tHead (aid int identity(1,1) primary key, b money)gocreate table tXct (txctid int identity(1,1), aid int references thead(aid), ob money, a money, newb money)goinsert tHead (b) values (0)----------------------------------------------------------------Begin the Test--set transaction isolation level serializable set transaction isolation level read committed --defaultbegin tran--read last closing balancedeclare @b money, @newb money, @a moneyset @a = 10select @b = b from tHead with (updlock) where aid = 1--simulate a delay - allow enough time for a concurrent session to run this codewaitfor delay '00:00:05.000'set @newb = @b+@ainsert tXct (aid, ob, a, newb) select 1, @b, @a, @newbupdate tHead set b = @newbfrom tHeadwhere aid = 1commit tran/*************************--In another window Launch this during the WAITFOR DELAY (same code without the waitfor)--set transaction isolation level serializable set transaction isolation level read committed --defaultbegin tran--read last closing balancedeclare @b money, @newb money, @a moneyset @a = 10select @b = b from tHead with (updlock) where aid = 1set @newb = @b+@ainsert tXct (aid, ob, a, newb) select 1, @b, @a, @newbupdate tHead set b = @newbfrom tHeadwhere aid = 1commit tran****************************/goselect * from tHeadselect * from tXct order by txctidgodrop table tXctdrop table tHead
Be One with the OptimizerTG