SQL Server Locks – A refresher on Locks : Part -1

Hi Friends,

Last week I was in talk with some junior DBA’s and they wanted to understand different types of SQL Server locks with examples. I believe this question is very common for most of the new entrants. Locking is very important feature of any RDBMS which helps us in working in multiuser environments. In our day to day work we do come across different types of locks when we check SQL DMV’s and so on; it is very much important to understand them at first place before we start troubleshooting anything. I thought it would be good, if I post my demonstration here on my blog which may be helpful to the ones with similar questions on their minds.

Locking is not a physical problem and a logical one. I’ll explain some basic lock types with an example under READ COMMITTED isolation mode and as you can understand for simplicity, detailed explanation of all lock types are beyond scope of this blog post. I’ve divided my posts in two parts and today in Part 1 we will cover following lock types;

  1. Shared Locks (S)
  2. Update Locks (U)
  3. Exclusive Locks (X)

The intention of this post is just to demonstrate lock modes with simple examples. We would be using sys.dm_tran_locks DMV to identify different lock types used in my example(s).

Shared Locks (S) : Shared locks are held when data is read under pessimistic concurrency model. Other transactions can read data but cannot modify records that are locked. When the locked data is read, shared locks are released. For demo, I’ve used WITH (HOLDLOCK) in select statement to simulate them;

1_SQL_Server_A_refresher_on_Locks_Part1

2_SQL_Server_A_refresher_on_Locks_Part1

Update Locks (U) : When you execute any DML statements, at first SQL engine has to find the data that it wants to modify; to avoid any lock conversion to deadlocks an update lock is used. You can say, these kinds of locks are a combination of shared and exclusive locks (covered next). Only one update lock is allowed on data at a time but this lock is unable to modify data on its own until converted to exclusive locks (covered next). I’ve used WITH (UPDLOCK) hint for demo purpose;

   

3_SQL_Server_A_refresher_on_Locks_Part1

4_SQL_Server_A_refresher_on_Locks_Part1

Exclusive Locks (X) : Exclusive locks are held to lock data that are modified by one transaction in order to prevent data modifications by other concurrent transactions. Only when you specify NOLOCK hint or read uncommitted isolation level you can read data. As discussed on update lock, for DML statements first data needs to read data before they modify them as such you will always observe exclusive locks after a shared lock on the data.

5_SQL_Server_A_refresher_on_Locks_Part1

6_SQL_Server_A_refresher_on_Locks_Part1

I’ll stop here today; watch out this space for next part where I will explore some more lock types with examples.

 

Regards

Kanchan Bhattacharyya

Like us on FaceBook Join the fastest growing SQL Server group on FaceBook

Follow me on TwitterFollow me on FaceBook

   

About Kanchan Bhattacharyya

Kanchan is an astute IT professional, a seasoned SQL Database Administrator with 13+ years of industry experience. A calculated risk taker with deep technical knowledge and has gained proficiency in database consulting and solutions across different environments. Kanchan holds MBA degree in IT and an International Executive MBA in Project Management. He is very passionate about SQL Server and holds MCSE (Data Platform), MCSA – SQL 2012, MCITP – SQL 2008 certifications. Currently he is focusing on cloud and latest releases of SQL Server. When not working, Kanchan likes to spend his time reading on new technical developments specifically on SQL Server and other related technologies.

View all posts by Kanchan Bhattacharyya →

Leave a Reply

Your email address will not be published.