News


Using Application Locks to Implement a Critical Section in T-SQL Code

Implementing critical sections is a problem we all stumble across from time-to-time.  I recently was trying to do one in SQL, and discovered that it was harder than I expected.  I didn't think of this method, which is a good one, by Louis Davidson.  He uses Application Locks (sp_getapplock, sp_releaseapplock) to solve the problem, which does exactly what you need, of course.

Usually critical sections are fairly easy to implement, but also must be handled carefully.  You must make sure that a) nothing can bypass your critical sections checks, and b) you always release your locks (or whatever necessary to notify waiters that you've left your critical section).  This method accomplishes the second, and warns you that you must handle most of the first issues yourself.

Still, even with that caveat, it's worth a look.

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
Thursday, May 24, 2007 2:52 PM

Feedback

No comments posted yet.


Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: