Minimizing deadlocks in SQL Server

Whenever more than one person has access to a database at the same time, the possibility of a deadlock occurring 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.

    Requires Free Membership to View

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.

About the Author

Barrie Sosinsky (barries@killerapps.com) is president of consulting company Sosinsky and Associates (Medfield MA). He has written extensively on a variety of computer topics. His company specializes in custom software (database and Web related), training and technical documentation.

This was first published in July 2001

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.