I was given this essay type question during an interview but did not get the job. The full question was as follows:
How are deadlocks detected and reported in an RDBMS? What are the transaction owner and application developer responsible for ensuring in both detection and prevention scenarios?
Best Answer
In SQL Server there is a separate thread that periodically (default 5 seconds, lower interval if a deadlock has just been detected) checks a list of waits for any cycles. I.e. it identifies the resource a thread is waiting for, then it finds the owner of that resource and recursively finds which resource that thread is in turn waiting for, thereby identifying threads that are waiting for each others resources.
If a deadlock is found then a victim is chosen to be killed using this algorithm:
You can find more information about SQL Servers deadlock detection here: http://msdn.microsoft.com/en-us/library/ms178104.aspx
The transaction owner/application developer is responsible for minimizing the risks of deadlocks occurring, and to do that they should: