Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

WAITFOR DELAY causes a lock Expand / Collapse
Author
Message
Posted Friday, May 17, 2013 10:57 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Today @ 8:26 AM
Points: 27, Visits: 198
Not an issue, but I found this curious. If I run the code:
USE AdventureWorks
GO
WAITFOR DELAY '00:00:15'

Then in a 2nd window run
EXEC sp_WhoIsActive 
@get_locks = 1,
@output_column_list = '[sql_text][sql_command][locks][%]'

It shows a lock?!?
The lock info is as follows
<Database name="AdventureWorks">
<Locks>
<Lock request_mode="S" request_status="GRANT" request_count="1" />
</Locks>
</Database>

I also tried updating the AdventureWorks.Production.Product table during the lock and it updated immediately with no issues. I don't understand:
1. Why is a lock is issued on a WAITFOR command?
2. How can I update even while there is a lock in place?

Just curious.
Thanks.
Post #1454101
Posted Friday, May 17, 2013 11:32 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 9:13 AM
Points: 40,609, Visits: 37,070
That's a shared database lock. Each and every running query, no matter what it's running, takes a shared database lock to ensure that the database can't be dropped while the query's running.

You can run updates with that lock there because the lock is not on a table, page or row, it's just a shared database lock.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1454132
Posted Friday, May 17, 2013 11:35 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Today @ 8:26 AM
Points: 27, Visits: 198
Ohhhh,

Well that makes sense. It had me seriously scratching my head. Thanks for the reply.
Post #1454135
Posted Friday, May 17, 2013 11:38 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 9:13 AM
Points: 40,609, Visits: 37,070
btw, the WAITFOR is not necessary to get that lock. USE ADVENTUREWORKS is sufficient.


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1454139
Posted Friday, May 17, 2013 11:51 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Today @ 8:26 AM
Points: 27, Visits: 198
I don't 'see' a lock when I USE ADVENTUREWORKS.
Is the lock only present while making the context switch?
Post #1454148
Posted Friday, May 17, 2013 11:57 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 9:13 AM
Points: 40,609, Visits: 37,070
If you don't see it, then there's something in sp_whoisactive that's filtering out those locks. Probably it's only showing sessions that are running, not ones that are sleeping and holding locks.

The lock is there from the time you connect to the database til the time you close the connection or change database context.

Trivial to show too.
Run in one window (and note the session_id)
USE AdventureWorks

Run in another window
SELECT  resource_type ,
resource_database_id ,
request_mode ,
request_type ,
request_status ,
request_session_id ,
request_owner_type
FROM sys.dm_tran_locks
WHERE request_session_id = <session id from the first window>




Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass



  Post Attachments 
DatabaseLock.jpg (54 views, 64.65 KB)
Post #1454153
Posted Friday, May 17, 2013 12:29 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Today @ 8:26 AM
Points: 27, Visits: 198
You are 100% correct. The lock showed up just like in your image. Very interesting. Thanks for taking the time to teach me something.
Post #1454168
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse