Home > SQL Server Tips > Database Management and Administration > Troubleshooting locking conflicts in SQL Server
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

DATABASE MANAGEMENT AND ADMINISTRATION

Troubleshooting locking conflicts in SQL Server


Baya Pavliashvili, Contributor
02.06.2002
Rating: --- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Troubleshooting locking problems is one of those activities that DBAs would much rather avoid, but most have to it do at one point or another. In fact, understanding database transactions and locking is what differentiates many beginners from the real pros.

So what is locking? As a fine Database Management System (DBMS), SQL Server needs to have a way to protect data integrity. In read-only databases, things are very simple: you populate your tables through regularly scheduled (perhaps weekly or nightly) loads and the users run SELECT statements against those tables. Since users are not allowed to make changes to the data -- no locks are acquired and everyone is happy. However, you've got to collect data prior to making it available for reading. The majority of your systems will be used for reading and modifying data at the same time.

When someone tries to modify a row of data in your table, SQL Server locks that record, so that no one else can modify the same record until the first data modification is complete. What if two users want to concurrently read the same record? Can one user change the record while another one is modifying the same record? The answer depends on how you configure your database settings.

By default, SQL Server runs under the TRANSACTION ISOLATION LEVEL of "READ COMMITED." This setting disallows reading of the record that is being modified. So if you have a batch job that modifies all records in a table, the rest of the database users will have to wait until your transaction is complete prior to being able to read any records in that table. This is accomplished by placing UPDATE locks on the rows being modified. In SQL Server 2000 there are several different types of locks that SQL Server can acquire, as summarized in the following table:

Lock TypeDescriptionCompatible with
SharedAcquired when reading recordsOther shared locks, update locks
UpdateAcquired prior to updating records. Ensures that no deadlocks occur.Shared locks
ExclusiveAcquired to perform data modifications through INSERT, UPDATE and DELETENot compatible with any other locks
Intent Shared (IS), Intent Exclusive (IX) AndShared with Intent Exclusive (SIX)Intent locks are used to establish a lock hierarchy. IS is acquired when transaction intends to read a subset of rows in the hierarchy by placing read locks. IX is acquired when transaction intends to modify a subset of rows by placing exclusive locks. SIX is acquired when all rows need to be read and at least some of them need to be modified. IS is compatible with everything but the exclusive locks. IX is compatible with other IX and IS locks. SIX is only compatible with IS locks.
Schema Modification (Sch-M)AndSchema Stability (Sch-S)Acquired during creation of database objects (Sch-M) and compiling stored procedures (Sch-S)All locks except Sch-S is not compatible with Sch-M
Bulk UpdateAcquired when bulk copying data into a table with TABLOCK hintSch-S and other bulk update locks only

If two lock types are incompatible with one another, one of the unlucky users will have to wait until the other transaction has completed its work. This event is referred to as blocking. Keep in mind that some blocking (that does not last very long) is normal and to be expected in transactional applications.

You can change the default TRANSACTION ISOLATION level with the one that most fits your needs. The less restrictive isolation levels allow reading of the data while it is being modified, which incidentally reduces the integrity of your data. Some of your users might see the values that have been changed. Some other users might see the changed values that haven't been committed to the disk yet. Therefore, be aware of the consequences of changing the isolation level and design your application accordingly.

If you have a multi-user transactional application blocking locks can get you in trouble quickly. While your app might perform fine with 10 users, when the number of concurrent users goes up to 50 your phone will be ringing off the hook. "I hit the CREATE RECORD button and my screen has been frozen ever since" will be the most frequently mentioned complaint. Some other users will complain that they tried connecting to the application several times to no avail. So what's the deal? How can we find out?

Fortunately, you can run a couple of system procedures to troubleshoot locking problems. The sp_who2 procedure shows you all current connections to your SQL Server as well as any problems with conflicting locks. This system procedure also provides the connection id (SPID), as well as the computer that initiated the connection. Usually the blocking locks have a chain effect -- if connection 3 is blocked by connection 2 and connection 4 requests the same resource, connection 4 will be blocked by 3. Unfortunately sp_who2 doesn't tell you the complete SQL statement executed by the offending connection, but it does tell you the type of operation performed. If you have system administrator privileges you can execute DBCC INPUTBUFFER (SPID) statement to get the last 150 characters of the SQL statement executed by the provided connection. The combination of sp_who2 and DBCC INPUTBUFFER statements will usually paint a very plain picture.

Occasionally the two ideas mentioned in the previous paragraph won't suffice. The problem is that if a SQL statement was executed as a remote procedure call DBCC INPUTBUFFER won't give you many details. It will simply show "RPC" in the output. In such case you have to use SQL Server Profiler to spy on the offending connection.

The Profiler has come long ways from its predecessor SQL Trace. It really is much easier to use and manage. However, be careful with this tool. As most other diagnostic tools, the Profiler has a tendency to track a lot of information by default -- much more than you'd want to decipher. And don't try to examine a day's worth of Profiler's output -- it'll be way to huge for most applications. Usually a trace file of an hour or two (during the peak hours) will do the job. If you have a locking problem while creating a trace file you can quickly find the SQL statement executed by the offending connection.

So far I've shown you how to identify the root and the cause of the locking problems. What happens next? The locking problems are often caused by the absence of appropriate indexes. If a transaction has to scan through a table with millions of rows it will be long and therefore will have to keep the locks on that table for a while. On the other hand, the transactions seeking particular values by taking advantage of indexes will be short and quick. The key to a successful application is keeping your transactions as short as possible. Sometimes this might mean rewriting the stored procedures and queries hitting your tables, but more often the key is placing appropriate indexes on each table.

In some systems the number of transactions might be too high to handle with a single server. In such case you should consider moving read operations (the reporting functionality) to a separate server. Keep in mind though that having two servers in place will require synchronizing data on two servers in some fashion. There are numerous ways of transferring data from one server to another, DTS and replication being the most effective and efficient. Be sure to consider pros and cons of each method of transferring data prior to making a decision.

So in this article I gave you a brief overview of potential locking problems, what causes them, how to detect and resolve them. Each database has its intricacies that cannot be predicted. But with the appropriate planning and design you can avoid most of the blocking issues. Using the powerful system procedures and diagnostic tools can help you troubleshoot the rest.

Reader Feedback

Heinz-Wilhelm F. writes: Why not use Oracle instead (any version from Oracle version 6 -- released in 1989 -- to the current version Oracle9i database) and stop worrying about locking issues any longer?

For More Information

  • Feedback: E-mail the editor with your thoughts about this tip.
  • More tips: Hundreds of free SQL Server tips and scripts.
  • Tip contest: Have a SQL Server tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
  • Forums: Ask your technical SQL Server questions--or help out your peers by answering them--in our active forums.
  • Best Web Links: SQL Server tips, tutorials, and scripts from around the Web.

Rate this Tip
To rate tips, you must be a member of SearchSQLServer.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Microsoft SQL Server Performance Monitoring and Tuning
SQL Server Mailbag: Asymmetric encryption, log shipping issues
Using traces in SQL Server Profiler
SQL Server Mailbag: CALs, witnesses and unwanted changes
SQL Server Mailbag: Data restoration and DB property management
Working with IntelliSense in SQL Server 2008 Management Studio
SQL Server Mailbag: Stored procedures, triggers and SSRS reports
Troubleshooting Distributed Transaction Coordinator errors in SQL Server
Clearing the Windows page file and its effect on server performance
Optimizing SQL Server indexes –- even when they're not your indexes
Performance implications of transaction log autogrowth in SQL Server

Database Management and Administration
Password cracking tools for SQL Server
Using traces in SQL Server Profiler
Meet compliance requirements with improved database security practices
Hardening the network and OS for SQL Server security
Securing the server and database in SQL Server
How SQL Server 2008 components impact SharePoint implementations
Troubleshooting Distributed Transaction Coordinator errors in SQL Server
Achieving high availability and disaster recovery with SharePoint databases
Clearing the Windows page file and its effect on server performance
Deploying a SQL Server virtual appliance for Microsoft Hyper-V

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
contiguity  (SearchSQLServer.com)
contiguous  (SearchSQLServer.com)
drilldown  (SearchSQLServer.com)
hashing  (SearchSQLServer.com)
hybrid online analytical processing  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

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.



SQL Server Development - .NET, C#, T-SQL, Visual Basic
HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts