Locking an entire table within a stored procedure

Locking an entire table within a stored procedure

Post by John » Fri, 16 Jan 2004 02:10:12



Forgive me for asking a question that gets asked a lot, but I don't know
where the archives of this list are stored. Is the following code the
best way to lock a table in a stored procedure while I do other queries
on the locked table and other tables ?  If I use a table hint of
"TABLOCKX" within a transaction, dosen't this lock the entire table
during the transaction, so I don't need to use the
SET TRANSACTION ISOLATION LEVEL.

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO
BEGIN TRAN
IF EXISTS (Select * From tblFoo WITH (TABLOCKX) Where ID=25)
BEGIN
    -- if get here then do other queries and updates on tblFoo, while
    -- table tblFOO is lock
END
ELSE
BEGIN
    Print "Records don't exist"
END
COMMIT TRAN

                        John
P.S.
Please respond to the list.

 
 
 

Locking an entire table within a stored procedure

Post by James Hoke » Fri, 16 Jan 2004 02:35:37


John,

TABLOCKX will be held until the end of the transaction.

Just a curiosity: why lock out the whole table?

James Hokes


Quote:> Forgive me for asking a question that gets asked a lot, but I don't know
> where the archives of this list are stored. Is the following code the
> best way to lock a table in a stored procedure while I do other queries
> on the locked table and other tables ?  If I use a table hint of
> "TABLOCKX" within a transaction, dosen't this lock the entire table
> during the transaction, so I don't need to use the
> SET TRANSACTION ISOLATION LEVEL.

> SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
> GO
> BEGIN TRAN
> IF EXISTS (Select * From tblFoo WITH (TABLOCKX) Where ID=25)
> BEGIN
>     -- if get here then do other queries and updates on tblFoo, while
>     -- table tblFOO is lock
> END
> ELSE
> BEGIN
>     Print "Records don't exist"
> END
> COMMIT TRAN

> John
> P.S.
> Please respond to the list.


 
 
 

Locking an entire table within a stored procedure

Post by John » Fri, 16 Jan 2004 03:52:27


James,

Thanks for responding to my message. I assume from your reply that I
don't need to set the TRANSACTION ISOLATION LEVEL.

I am writting a low volumn scheduling program. The tblFoo contains the
list of reservations for time on our instruments (NMR, GC's, etc.). I
want to lock the table while I scan for reservation conflicts. If there
are no conflicts then I will make an entry in the table for the
reservation. This will all be done in a stored procedure and should not
take up much time. Keep in mind this is a low transaction system.

                        John



> John,

> TABLOCKX will be held until the end of the transaction.

> Just a curiosity: why lock out the whole table?

> James Hokes



> > Forgive me for asking a question that gets asked a lot, but I don't know
> > where the archives of this list are stored. Is the following code the
> > best way to lock a table in a stored procedure while I do other queries
> > on the locked table and other tables ?  If I use a table hint of
> > "TABLOCKX" within a transaction, dosen't this lock the entire table
> > during the transaction, so I don't need to use the
> > SET TRANSACTION ISOLATION LEVEL.

> > SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
> > GO
> > BEGIN TRAN
> > IF EXISTS (Select * From tblFoo WITH (TABLOCKX) Where ID=25)
> > BEGIN
> >     -- if get here then do other queries and updates on tblFoo, while
> >     -- table tblFOO is lock
> > END
> > ELSE
> > BEGIN
> >     Print "Records don't exist"
> > END
> > COMMIT TRAN

> > John
> > P.S.
> > Please respond to the list.

 
 
 

Locking an entire table within a stored procedure

Post by James Hoke » Fri, 16 Jan 2004 06:36:23


John,

I see. There is also the new sp_getapplock and sp_releaseapplock to use for
just this purpose.
(Note: this is a 'logical' lock, it doesn't actually lock the rows in
question)

Example:

/*Lock #25*/
EXEC sp_getapplock



/*DO WORK*/

/*Release #25*/
EXEC sp_releaseapplock


This has the added advantage of 'locking' the 'virtual resource', but
allowing reads on the data in the meantime.

James Hokes


> James,

> Thanks for responding to my message. I assume from your reply that I
> don't need to set the TRANSACTION ISOLATION LEVEL.

> I am writting a low volumn scheduling program. The tblFoo contains the
> list of reservations for time on our instruments (NMR, GC's, etc.). I
> want to lock the table while I scan for reservation conflicts. If there
> are no conflicts then I will make an entry in the table for the
> reservation. This will all be done in a stored procedure and should not
> take up much time. Keep in mind this is a low transaction system.

> John



> > John,

> > TABLOCKX will be held until the end of the transaction.

> > Just a curiosity: why lock out the whole table?

> > James Hokes



> > > Forgive me for asking a question that gets asked a lot, but I don't
know
> > > where the archives of this list are stored. Is the following code the
> > > best way to lock a table in a stored procedure while I do other
queries
> > > on the locked table and other tables ?  If I use a table hint of
> > > "TABLOCKX" within a transaction, dosen't this lock the entire table
> > > during the transaction, so I don't need to use the
> > > SET TRANSACTION ISOLATION LEVEL.

> > > SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
> > > GO
> > > BEGIN TRAN
> > > IF EXISTS (Select * From tblFoo WITH (TABLOCKX) Where ID=25)
> > > BEGIN
> > >     -- if get here then do other queries and updates on tblFoo, while
> > >     -- table tblFOO is lock
> > > END
> > > ELSE
> > > BEGIN
> > >     Print "Records don't exist"
> > > END
> > > COMMIT TRAN

> > > John
> > > P.S.
> > > Please respond to the list.

 
 
 

Locking an entire table within a stored procedure

Post by John » Sat, 17 Jan 2004 05:19:21


James,

Yes, I saw that statement but was not sure what the definition of a
resource was. Can a resource name be a name of a table ? In your
example is "Foo #25" a table name ?

Where did you get the information that this is a "logical" lock. I don't
see this in the books on line docs.

                 John


says...

> John,

> I see. There is also the new sp_getapplock and sp_releaseapplock to use for
> just this purpose.
> (Note: this is a 'logical' lock, it doesn't actually lock the rows in
> question)

> Example:

> /*Lock #25*/
> EXEC sp_getapplock



> /*DO WORK*/

> /*Release #25*/
> EXEC sp_releaseapplock


> This has the added advantage of 'locking' the 'virtual resource', but
> allowing reads on the data in the meantime.

> James Hokes



> > James,

> > Thanks for responding to my message. I assume from your reply that I
> > don't need to set the TRANSACTION ISOLATION LEVEL.

> > I am writting a low volumn scheduling program. The tblFoo contains the
> > list of reservations for time on our instruments (NMR, GC's, etc.). I
> > want to lock the table while I scan for reservation conflicts. If there
> > are no conflicts then I will make an entry in the table for the
> > reservation. This will all be done in a stored procedure and should not
> > take up much time. Keep in mind this is a low transaction system.

> > John



> > > John,

> > > TABLOCKX will be held until the end of the transaction.

 
 
 

Locking an entire table within a stored procedure

Post by James Hoke » Sat, 17 Jan 2004 07:33:25


John,


that it's a physical table or anything.

So, let's say you wanted to 'lock' a customer account while certain
operations were performed on it. You could use the customer's AccountNumber
or CustomerID to lock the entire logical set of operations. So, for customer

The point is, if another user came along to perform the same process, and
lock 'Customer A12345', the lock would be denied if you had an 'Exclusive'
one on it already. Then, when you were finished up, you could release the
lock you had on 'Customer A12345'.

IMO, this is meant to take the place of having kludges like putting a
[RecordLocked] column on your Customer table, and toggling it on/off for
example.
Also, the advantage is that you could apply this so that the customer could
be read, just not edited, etc.. Imagination is the only limit with this new
functionality.

If you don't see this in BOL, maybe you need to download the latest version.

James Hokes


> James,

> Yes, I saw that statement but was not sure what the definition of a
> resource was. Can a resource name be a name of a table ? In your
> example is "Foo #25" a table name ?

> Where did you get the information that this is a "logical" lock. I don't
> see this in the books on line docs.

>                  John


> says...
> > John,

> > I see. There is also the new sp_getapplock and sp_releaseapplock to use
for
> > just this purpose.
> > (Note: this is a 'logical' lock, it doesn't actually lock the rows in
> > question)

> > Example:

> > /*Lock #25*/
> > EXEC sp_getapplock



> > /*DO WORK*/

> > /*Release #25*/
> > EXEC sp_releaseapplock


> > This has the added advantage of 'locking' the 'virtual resource', but
> > allowing reads on the data in the meantime.

> > James Hokes



> > > James,

> > > Thanks for responding to my message. I assume from your reply that I
> > > don't need to set the TRANSACTION ISOLATION LEVEL.

> > > I am writting a low volumn scheduling program. The tblFoo contains the
> > > list of reservations for time on our instruments (NMR, GC's, etc.). I
> > > want to lock the table while I scan for reservation conflicts. If
there
> > > are no conflicts then I will make an entry in the table for the
> > > reservation. This will all be done in a stored procedure and should
not
> > > take up much time. Keep in mind this is a low transaction system.

> > > John



> > > > John,

> > > > TABLOCKX will be held until the end of the transaction.

 
 
 

Locking an entire table within a stored procedure

Post by James Hoke » Sat, 17 Jan 2004 08:02:50


John,


that it's a physical table or anything.

So, let's say you wanted to 'lock' a customer account while certain
operations were performed on it. You could use the customer's AccountNumber
or CustomerID to lock the entire logical set of operations. So, for customer

The point is, if another user came along to perform the same process, and
lock 'Customer A12345', the lock would be denied if you had an 'Exclusive'
one on it already. Then, when you were finished up, you could release the
lock you had on 'Customer A12345'.

IMO, this is meant to take the place of having kludges like putting a
[RecordLocked] column on your Customer table, and toggling it on/off for
example.
Also, the advantage is that you could apply this so that the customer could
be read, just not edited, etc.. Imagination is the only limit with this new
functionality.

If you don't see this in BOL, maybe you need to download the latest version.

James Hokes


> James,

> Yes, I saw that statement but was not sure what the definition of a
> resource was. Can a resource name be a name of a table ? In your
> example is "Foo #25" a table name ?

> Where did you get the information that this is a "logical" lock. I don't
> see this in the books on line docs.

>                  John


> says...
> > John,

> > I see. There is also the new sp_getapplock and sp_releaseapplock to use
for
> > just this purpose.
> > (Note: this is a 'logical' lock, it doesn't actually lock the rows in
> > question)

> > Example:

> > /*Lock #25*/
> > EXEC sp_getapplock



> > /*DO WORK*/

> > /*Release #25*/
> > EXEC sp_releaseapplock


> > This has the added advantage of 'locking' the 'virtual resource', but
> > allowing reads on the data in the meantime.

> > James Hokes



> > > James,

> > > Thanks for responding to my message. I assume from your reply that I
> > > don't need to set the TRANSACTION ISOLATION LEVEL.

> > > I am writting a low volumn scheduling program. The tblFoo contains the
> > > list of reservations for time on our instruments (NMR, GC's, etc.). I
> > > want to lock the table while I scan for reservation conflicts. If
there
> > > are no conflicts then I will make an entry in the table for the
> > > reservation. This will all be done in a stored procedure and should
not
> > > take up much time. Keep in mind this is a low transaction system.

> > > John



> > > > John,

> > > > TABLOCKX will be held until the end of the transaction.