Home All Groups Group Topic Archive Search About

Using [ sp_getapplock ]

Author
23 Mar 2006 3:09 PM
rmg66
I'm sorry if I'm being dense. But I don't quite understand how to use sp_getapplock.

Is it supposed to lock access to any db object (e.g. a particular stored procedure) until it is unlocked?

When I tested it. It didn't seeem to do what I expected....
I used it to lock a table object and then attempted to update the table. I did it inside a transaction.
I expected the table to reject the update but it didn't.

Am I missing something?

Can someone give me an example of how I could use this sp to prevent a stored procedure from being called from another user (or event the same user) while it is Currently Executing?

Thanks,

Robert

Author
23 Mar 2006 3:27 PM
David Browne
sp_getapplock is like a database mutex, or critical section.  It doesn't lock any particular database object, but instead creates a new named lock.  Any other session attempting to run sp_getapplock with the same argument will be serialized.  The BOL has an example.

David
  "rmg66" <rgwathney__xXx__primepro.com> wrote in message news:%23XxumuoTGHA.1576@tk2msftngp13.phx.gbl...
  I'm sorry if I'm being dense. But I don't quite understand how to use sp_getapplock.

  Is it supposed to lock access to any db object (e.g. a particular stored procedure) until it is unlocked?

  When I tested it. It didn't seeem to do what I expected....
  I used it to lock a table object and then attempted to update the table. I did it inside a transaction.
  I expected the table to reject the update but it didn't.

  Am I missing something?

  Can someone give me an example of how I could use this sp to prevent a stored procedure from being called from another user (or event the same user) while it is Currently Executing?

  Thanks,

  Robert
Author
23 Mar 2006 3:40 PM
JT
If basically what you need is to exclusively lock database objects (such as a specific table) during a transaction, then consider using 'locking hints' like TABLOCKX or setting the transaction isolation level to serializable. There are many levels of control over locking, and you will need to choose the minimal level that meets your specific case usage needs without unnecessarily blocking other processes.
  "rmg66" <rgwathney__xXx__primepro.com> wrote in message news:%23XxumuoTGHA.1576@tk2msftngp13.phx.gbl...
  I'm sorry if I'm being dense. But I don't quite understand how to use sp_getapplock.

  Is it supposed to lock access to any db object (e.g. a particular stored procedure) until it is unlocked?

  When I tested it. It didn't seeem to do what I expected....
  I used it to lock a table object and then attempted to update the table. I did it inside a transaction.
  I expected the table to reject the update but it didn't.

  Am I missing something?

  Can someone give me an example of how I could use this sp to prevent a stored procedure from being called from another user (or event the same user) while it is Currently Executing?

  Thanks,

  Robert
Author
23 Mar 2006 4:03 PM
Tibor Karaszi
See the other post I posted with sample code how to use this.

"rmg66" <rgwathney__xXx__primepro.com> wrote in message
news:%23XxumuoTGHA.1576@tk2msftngp13.phx.gbl...
I'm sorry if I'm being dense. But I don't quite understand how to use sp_getapplock.

Is it supposed to lock access to any db object (e.g. a particular stored procedure) until it is
unlocked?

When I tested it. It didn't seeem to do what I expected....
I used it to lock a table object and then attempted to update the table. I did it inside a
transaction.
I expected the table to reject the update but it didn't.

Am I missing something?

Can someone give me an example of how I could use this sp to prevent a stored procedure from being
called from another user (or event the same user) while it is Currently Executing?

Thanks,

Robert

AddThis Social Bookmark Button