Whenever more than one person has access to a database at the same time, the possibility of a deadlock occurring...
By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
is high. A deadlock occurs when a user starts a transaction and locks the records they are using. Another user wants to access those records and cannot, so they begin a transaction and lock records in another part of the database, where the first user needs to access a record as well. Until the records are released and mutual blockings are cleared, a deadlock results.
SQL Server has a built in mechanism for detecting and resolving deadlocks. Once it finds a deadlock situation, SQL Server will assess the execution cost, and select which transaction will be executed. It will notify the other processes waiting in the deadlock that an error (error 1205) has occurred and will let them know they have been selected as victims in the deadlock situation. If the processes are blocking one another in a circular type reference, SQL Server will decide which process will be selected to break the deadlock. The one with the least overall cost usually wins. When a transaction is selected as the victim in a deadlock, the process is cancelled and changes applied are rolled back. The calling application, however, can usually resend the transaction once the previous locks have been removed.
To keep deadlock occurrences to a minimum you can make a concerted effort to keep transactions as short as possible. Avoid user interaction from inside transactions. Begin a transaction when required but release it as quickly as possible. Use the same order when accessing resources. Check for potential circular references. It is recommended that you use the READ COMMITTED isolation level whenever possible. It produces fewer locks than higher isolation levels. Avoid SERIALIZABLE as much as you can. Bind multiple connections to share the same locking space. Execute the stored procedure sp_bindsession to keep more than one session in the same transaction. Although not guaranteed to eliminate deadlocks completely, these steps should help you keep deadlocks to a minimum when using SQL Server.