Solved

How do I acquire a database lock?

Posted on 2009-06-29
6
283 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Database ERD 4 32
Are triggers slow? 7 15
Need return values from a stored procedure 8 23
MS SQL SERVER and ADODB.commands 8 23
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

820 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