<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>The Case of the Missing Shared Locks</title><link>http://sqlblog.com/blogs/paul_white/archive/2010/11/01/read-committed-shared-locks-and-rollbacks.aspx</link><description>In this post I cover a little-known locking optimization that provides a surprising answer to the question: “If I hold an exclusive lock on a row, can another transaction running at the default read committed isolation level read it?” Most people would</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: Read Committed, Shared Locks, and Rollbacks</title><link>http://sqlblog.com/blogs/paul_white/archive/2010/11/01/read-committed-shared-locks-and-rollbacks.aspx#30039</link><pubDate>Mon, 01 Nov 2010 06:47:47 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:30039</guid><dc:creator>Uri Dimant</dc:creator><description>&lt;p&gt;Paul&lt;/p&gt;
&lt;p&gt;SQL Server 2005 (SP3)&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;&amp;gt;Perhaps unexpectedly, this query does not block – the result is &amp;gt;&amp;gt;&amp;gt;returned immediately, despite the exclusive lock.&lt;/p&gt;
&lt;p&gt;Running the below in the second connection IS blocked&lt;/p&gt;
&lt;p&gt;SELECT &amp;nbsp;D.some_key, &amp;nbsp; &amp;nbsp; &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;D.some_value FROM &amp;nbsp;&lt;/p&gt;
&lt;p&gt;dbo.Demo D WITH (ROWLOCK, READCOMMITTEDLOCK);&lt;/p&gt;
&lt;p&gt;What I am missing?&lt;/p&gt;
</description></item><item><title>re: Read Committed, Shared Locks, and Rollbacks</title><link>http://sqlblog.com/blogs/paul_white/archive/2010/11/01/read-committed-shared-locks-and-rollbacks.aspx#30042</link><pubDate>Mon, 01 Nov 2010 08:06:55 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:30042</guid><dc:creator>Paul White</dc:creator><description>&lt;p&gt;Hi Uri,&lt;/p&gt;
&lt;p&gt;If you created the Demo table in tempdb, you need to run a manual CHECKPOINT after creating the table and inserting the test row.&lt;/p&gt;
&lt;p&gt;I've updated the script to reflect that requirement - thanks for the feedback :)&lt;/p&gt;
&lt;p&gt;Paul&lt;/p&gt;
</description></item><item><title>re: Read Committed, Shared Locks, and Rollbacks</title><link>http://sqlblog.com/blogs/paul_white/archive/2010/11/01/read-committed-shared-locks-and-rollbacks.aspx#30046</link><pubDate>Mon, 01 Nov 2010 10:12:28 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:30046</guid><dc:creator>Uri Dimant</dc:creator><description>&lt;p&gt;Hi Paul&lt;/p&gt;
&lt;p&gt;Even running SELECT in the second connection without the hints does not block and we get result immediately&lt;/p&gt;
</description></item><item><title>re: Read Committed, Shared Locks, and Rollbacks</title><link>http://sqlblog.com/blogs/paul_white/archive/2010/11/01/read-committed-shared-locks-and-rollbacks.aspx#30049</link><pubDate>Mon, 01 Nov 2010 10:34:33 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:30049</guid><dc:creator>Paul White</dc:creator><description>&lt;p&gt;Uri,&lt;/p&gt;
&lt;p&gt;Yes, that's right. &amp;nbsp;The hints aren't required for the optimization to kick in - I added them to make it clear that shared row locks are being taken. &amp;nbsp;Glad it's working for you now.&lt;/p&gt;
&lt;p&gt;Paul&lt;/p&gt;
</description></item><item><title>re: The Case of the Missing Shared Locks</title><link>http://sqlblog.com/blogs/paul_white/archive/2010/11/01/read-committed-shared-locks-and-rollbacks.aspx#42388</link><pubDate>Mon, 19 Mar 2012 15:28:09 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:42388</guid><dc:creator>Rishabh K</dc:creator><description>&lt;p&gt;Hi Paul,&lt;/p&gt;
&lt;p&gt;I tried the same thing on one of my database but as expected the second session was blocked. However it works in tempdb &lt;/p&gt;
&lt;p&gt;Use MyDatabase;&lt;/p&gt;
&lt;p&gt;GO &lt;/p&gt;
&lt;p&gt;CREATE TABLE TEST(ID int PRIMARY KEY,Name varchar(50))&lt;/p&gt;
&lt;p&gt;GO &lt;/p&gt;
&lt;p&gt;INSERT INTO TEST values (1,'Rishabh')&lt;/p&gt;
&lt;p&gt;GO &lt;/p&gt;
&lt;p&gt;--Session 1 &lt;/p&gt;
&lt;p&gt;BEGIN TRAN &lt;/p&gt;
&lt;p&gt;select * from test with (XLOCK)&lt;/p&gt;
&lt;p&gt;--session 2 &lt;/p&gt;
&lt;p&gt;select * from test with(rowlock,readcommittedlock)&lt;/p&gt;
&lt;p&gt;A little doubt..In connection 1, the data is still in uncommitted mode(not available to others as Read committed ensures).&lt;/p&gt;
&lt;p&gt;What I think is even if we specify rowlock in session 2, sql server will anyhow go for the rowlock assuming that there is only a single row ,so it will try to get a shared lock on that row and due to the fact that the row is X locked by the session 1, it will be blocked.&lt;/p&gt;
&lt;p&gt;I know that IS is compatible with IX but here there is only one key that really needs the access(one with X lock in session 1 and one with S lock in session 2)&lt;/p&gt;
&lt;p&gt;Am I missing anything or tempdb works in a different way as compared to other user databases ??&lt;/p&gt;
&lt;p&gt;Thanks for all the great post!!!&lt;/p&gt;</description></item><item><title>re: The Case of the Missing Shared Locks</title><link>http://sqlblog.com/blogs/paul_white/archive/2010/11/01/read-committed-shared-locks-and-rollbacks.aspx#42389</link><pubDate>Mon, 19 Mar 2012 15:31:17 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:42389</guid><dc:creator>Rishabh K</dc:creator><description>&lt;p&gt;I think I missed the checkpoint run..Thanks again &lt;/p&gt;</description></item></channel></rss>