Solved

How do I acquire a database lock?

Posted on 2009-06-29
6
280 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

746 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

9 Experts available now in Live!

Get 1:1 Help Now