Deadlock means that two transaction each lock a resource say a row in a Table.
Perhaps in the usp_UpdateProductPrice proc (the name sounds good anyway) you do the following
begin tran
update Product
Update QuoteHeader -- to update all of the Quotes (that are not yet sales) with the new price
Commit tran
Then in another proc perhaps named usp_UpdateQuoteHeader (to update some other info)
begin tran
SELECT FROM QuoteHeader
select FROM Product
Update QuoteHeader
Commit Tran
Well when usp_UpdateProductPrice and usp_UpdateQuoteHeader happen to run at the same time and are affecting the same rows
Here is what happens
usp_UpdateProductPrice usp_UpdateQuoteHeader
Exclusive Lock on Product Shared Lock on QuoteHeader
Intent Exclusive on QuoteHeader Intent Shared on Product
(can't get the exclusive (can't get the shared because of the exclusive)
because of the shared lock)
We get a deadlock, because usp_UpdateProductPrice won't let go of Product until it gets QuoteHeader
and usp_UpdateQuoteHeader won't let go of QuoteHeader until it gets Product.
So SQL Server will randomly pick on transaction and call it the deadlock victim and kill it.
However, this takes 30 seconds.
The solution is to change on of the procs so that they both access things in the same order
Then in another proc perhaps named usp_UpdateQuoteHeader (to update some other info)
SELECT FROM QuoteHeader -- get the product info
begin tran
select FROM Product -- put this first in the transaction
SELECT FROM QuoteHeader
Update QuoteHeader
Commit Tran
Now we get the following:
usp_UpdateProductPrice usp_UpdateQuoteHeader
Exclusive Lock on Product Intent Shared on Product
Exclusive Lock on QuoteHeader Blocking waiting for Shared Lock on Product
Commits and releases locks
Shared Lock on Product
Shared Lock on QuoteHeader
Exclusive Lock on QuoteHeader
Commits and releases locks
OR we get
usp_UpdateProductPrice usp_UpdateQuoteHeader (if this is slightly ahead)
Shared Lock on Product
Intent Exclusive on Product Shared Lock on QuoteHeader
Exclusive Lock on QuoteHeader
Commits and releases locks
Exclusive Lock on Product
Exclusive Lock on QuoteHeader
Commits and releases locks
Use Profiler and your error reporting from the client app to sniff out which
SQL Transactions are causing the deadlocks
David Lundell
Principal Consultant and Trainer
www.mutuallybeneficial.com