how to lock and unlock a table exclusively?

hello group,

I have gone over few articles and samples on the web and learned something about locking a table and it types. However, not sure why I cannot find a solid answer on how to unlock a table.

my assumption was that unlock must be the answer but it doesn't work in sql server (I'm using 2008 R2). so far I have locked a table (exclusively since need to avoid any read/write on table):

USE DB1
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO
BEGIN TRANSACTION
SELECT 1 FROM test WITH (tablockx)
GO

Open in new window


eventually I had to kill the server process id after finding process id with kill command. now, question is what is the best and graceful to exit from lock state?

Thanks.
akohanAsked:
Who is Participating?
 
wdosanjosConnect With a Mentor Commented:
Yes, that's the only approach to release the locks (i.e. issue a COMMIT or a ROLLBACK).
0
 
wdosanjosCommented:
You should COMMIT or ROLLBACK the transaction.
0
 
akohanAuthor Commented:
I tried and noticed COMMIT will work properly but is this a good and optimized approach (considering code above and commit)?

Regards.
0
 
akohanAuthor Commented:
Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.