Solved

How do I acquire a database lock?

Posted on 2009-06-29
6
281 Views
Last Modified: 2012-05-07
For some maintenance I need to drop a table and recreate it. However, it's possible another process could try to insert into this table during that short period of time. I would like to lock the table so the other process will wait, but that isn't going to work with dropping the table. I'd like to acquire a database lock for the (short) duration of this process so the other process will just wait, but I cannot figure out how to acquire one intentionally.

(This is 2005 and above if that matters)
0
Comment
Question by:turbohappy
  • 3
  • 2
6 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 500 total points
ID: 24742223
You can either use TABLOCK / TABLOCKX hint to achieve your objective.

If tablex is your table name then issue

SELECT * FROM tablex WITH (TABLOCK)

would issue a lock on the table.

TABLOCKX Specifies that an exclusive lock is taken on the table until the transaction completes whereas TABLOCK Specifies that a lock is taken on the table and held until the end-of-statement.

0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24742230
This option is applicable on both SQL Server 2005 and 2008

http://msdn.microsoft.com/en-us/library/ms187373.aspx
0
 

Author Comment

by:turbohappy
ID: 24742255
TABLOCKX will hold even if I drop the table in my transaction?
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24742769
TABLOCKX or TABLOCK are all Table hints with respect to a transaction / session only.
So if you issue a TABLOCKX in the beginning of your transaction / Session, then that particular object is locked for your session / transaction.

And hence you will be able to drop the table within the transaction.

Hope I clarified you out.
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 24743493
Here is what I'd try to do if I wanted to make sure *specific* objects have no transactions modifying them...

> Create a filegroup called CANTTOUCHTHIS.  
> Anytime you want an object to be unmodified for a period of time, migrate the object to the filegroup, by recreating its clustered index, table, and non clustered index...Put the filegroup back in READ_ONLY
> Once done, bring back the table in its original filegroup.

That should work but I have not tried to be honnest...hth
0
 

Author Closing Comment

by:turbohappy
ID: 31598220
Wow, thanks! I guess I should have tested it first, I just assumed it would kill the lock to drop the table. Works perfectly.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now