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.
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 AuthorBarrie Sosinsky (email@example.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