SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


WAITFOR DELAY causes a lock


WAITFOR DELAY causes a lock

Author
Message
facemann
facemann
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 286
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. :-)
GilaMonster
GilaMonster
SSC Guru
SSC Guru (151K reputation)SSC Guru (151K reputation)SSC Guru (151K reputation)SSC Guru (151K reputation)SSC Guru (151K reputation)SSC Guru (151K reputation)SSC Guru (151K reputation)SSC Guru (151K reputation)

Group: General Forum Members
Points: 151235 Visits: 45881
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, MVP, M.Sc (Comp Sci)
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


facemann
facemann
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 286
Ohhhh,

Well that makes sense. It had me seriously scratching my head. Thanks for the reply. Cool
GilaMonster
GilaMonster
SSC Guru
SSC Guru (151K reputation)SSC Guru (151K reputation)SSC Guru (151K reputation)SSC Guru (151K reputation)SSC Guru (151K reputation)SSC Guru (151K reputation)SSC Guru (151K reputation)SSC Guru (151K reputation)

Group: General Forum Members
Points: 151235 Visits: 45881
btw, the WAITFOR is not necessary to get that lock. USE ADVENTUREWORKS is sufficient.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
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


facemann
facemann
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 286
I don't 'see' a lock when I USE ADVENTUREWORKS.
Is the lock only present while making the context switch?
GilaMonster
GilaMonster
SSC Guru
SSC Guru (151K reputation)SSC Guru (151K reputation)SSC Guru (151K reputation)SSC Guru (151K reputation)SSC Guru (151K reputation)SSC Guru (151K reputation)SSC Guru (151K reputation)SSC Guru (151K reputation)

Group: General Forum Members
Points: 151235 Visits: 45881
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, MVP, M.Sc (Comp Sci)
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


Attachments
DatabaseLock.jpg (121 views, 64.00 KB)
facemann
facemann
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 286
You are 100% correct. The lock showed up just like in your image. Very interesting. Thanks for taking the time to teach me something. Cool
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search