Author |
Topic |
ryanlcs
Yak Posting Veteran
62 Posts |
Posted - 2012-01-12 : 02:31:01
|
HiI know that the command TABLOCKX is use to gain exclusive lock of a table. After the table is locked, when will it be unlocked?Considering I have these 2 statements:Select * From TableA with (TABLOCKX)Insert Into Tabla A Values(@newValue) When I applied TABLOCKX on Select statement, when will the TableA be unlocked? After Statement 1 or 2?When I am looking for is that the TableA is unlocked after statement 2 as no other process can access TableA without both statement being executed.Kindly advice. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-12 : 03:14:09
|
the locking hint will have scope only for select statement. so once select statement is executed it should get released------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-12 : 03:14:47
|
if you want tableA to be locked why not wrap both the statement inside a transaction?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2012-01-12 : 05:00:13
|
If you want the X Lock held until the update, this will do itBEGIN TRANSACTION BEGIN TRYSelect * From TableA with (TABLOCKX)Insert Into Tabla A Values(@newValue)COMMIT TRANSACTIONEND TRYBEGIN CATCHROLLBACK TRANSACTION-- any other error handling goes hereEND CATCH--Gail ShawSQL Server MVP |
 |
|
ryanlcs
Yak Posting Veteran
62 Posts |
Posted - 2012-01-12 : 19:22:37
|
This seems very good solution. Will try it out.Sorry, I am not familiar with TRANSACTION. What if I apply the TRANSACTION structure and remove the TABLOCKX, does it produce same result? |
 |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2012-01-12 : 19:36:29
|
No, without the TablockX hint it'll be a shared table lock, not an exclusive. So it'll prevent anyone else from inserting, updating or deleting, but will allow others to read and, if this piece of code could be run in parallel by multiple users it could deadlock.--Gail ShawSQL Server MVP |
 |
|
ryanlcs
Yak Posting Veteran
62 Posts |
Posted - 2012-01-12 : 19:47:35
|
So by applying the TRAN and TABLOCKX together, whenever there is multiple access, it should apply the queue concept is it? If yes, this is wnat I am looking for. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-13 : 03:32:16
|
quote: Originally posted by ryanlcs So by applying the TRAN and TABLOCKX together, whenever there is multiple access, it should apply the queue concept is it? If yes, this is wnat I am looking for.
yep...only after lock is released it can be accessed by next process------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|