Solved

How do I acquire a database lock?

Posted on 2009-06-29
6
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

691 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