Solved

How do I acquire a database lock?

Posted on 2009-06-29
6
282 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

803 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