If your application has lot of traffic and it involves databases then you might run into Deadlock scenarios. A deadlock happens when 2 threads are holding a lock for some resource and waiting on each other to release the lock so that they can continue.
In Sql Server deadlock is handled by the SQL Server Lock monitor thread and when a deadlock occurs it decides which process to terminate based on the priority of the process.
One of the most important thing you need to do, while setting up the SQL Server is turning on the TraceFlag 1222 in the startup parameters. What this does is whenever a Deadlock occurs it will add the deadlock information in the errorlog file, which you can use to analyse why deadlock occurred.
The error log is pretty detailed and you can figure out a lot of information from that. Its like a postmortem analysis, when trying to figure of a issue in production. There is a crime scene, culprit, symptoms and victims.
As you can see from the ErrorLog above, it gives you which process is holding which resource and what type of lock it has. It gives you which Sql Statements caused the deadlocks and the sqlHandle, process Id. It is pretty self descriptive but very informative.
The picture below shows diagrammatically, same info whats there in the error logs. It has helped me a lot to create a picture whenever a deadlocks occurs so that I can figure what exactly is happening.
These are some ways you can try to handle deadlocks situations.
1) Keep the transactions small.
2) Do nibble deletes/updates/inserts i.e fews records at a time and repeating the operation.
3)Using a Retry logic and see if it succeeds the second time/third time etc.
4) Adding WAIT FOR for sometime. so that the transaction which holds the local can complete.
I love SQL Server and love thinking in terms of Sets. Still trying to learn !
Hope this helps someone
Thanks,
Yash
In Sql Server deadlock is handled by the SQL Server Lock monitor thread and when a deadlock occurs it decides which process to terminate based on the priority of the process.
One of the most important thing you need to do, while setting up the SQL Server is turning on the TraceFlag 1222 in the startup parameters. What this does is whenever a Deadlock occurs it will add the deadlock information in the errorlog file, which you can use to analyse why deadlock occurred.
The error log is pretty detailed and you can figure out a lot of information from that. Its like a postmortem analysis, when trying to figure of a issue in production. There is a crime scene, culprit, symptoms and victims.
As you can see from the ErrorLog above, it gives you which process is holding which resource and what type of lock it has. It gives you which Sql Statements caused the deadlocks and the sqlHandle, process Id. It is pretty self descriptive but very informative.
The picture below shows diagrammatically, same info whats there in the error logs. It has helped me a lot to create a picture whenever a deadlocks occurs so that I can figure what exactly is happening.
These are some ways you can try to handle deadlocks situations.
1) Keep the transactions small.
2) Do nibble deletes/updates/inserts i.e fews records at a time and repeating the operation.
3)Using a Retry logic and see if it succeeds the second time/third time etc.
4) Adding WAIT FOR for sometime. so that the transaction which holds the local can complete.
I love SQL Server and love thinking in terms of Sets. Still trying to learn !
Hope this helps someone
Thanks,
Yash