Click here to Skip to main content
15,882,055 members
Articles / Database Development / SQL Server
Tip/Trick

SQL Server Lock Critical Section

Rate me:
Please Sign up or sign in to vote.
2.33/5 (3 votes)
23 Oct 2015CPOL1 min read 15.1K   1   7
Marks a statement block as a critical section by obtaining the mutual-exclusion lock

Introduction

This script ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread tries to enter a locked code, it will wait, block, until the critical section is released.

Background

First, we review concepts of temporary tables.

There are two types of temporary tables: local and global. Local temporary tables are visible only to their creators during the same connection to an instance of SQL Server as when the tables were first created or referenced. Local temporary tables are deleted after the user disconnects from the instance of SQL Server. Global temporary tables are visible to any user and any connection after they are created, and are deleted when all users that are referencing the table disconnect from the instance of SQL Server.

Read more here.

Using the Code

With this script, we ensure that the code within the BEGIN TRY will be executed by a process at a time.
We use a global temporary table as a flag to see if there is another process within the critical section.
OBJECT_ID SQL function checks for the table, if it does not return null there.
If the global temporary table does not exist, it enters the critical section, but waits in a loop until the section is unlocked.

The WAITFOR DELAY statement is used for the loop to wait a reasonable time to re-verify income section.
If an error occurs, go to the CATCH section and delete the temporary table releasing lock on the critical section.

SQL
WHILE (1=1)
BEGIN
      IF (OBJECT_ID('tempdb..##LOCK_SECTION_A') IS NULL) BEGIN     
            BEGIN TRY   
                        CREATE TABLE ##LOCK_SECTION_A (ID INT)
                            
                        /*-------------------*/    
                        --SCRIPT HERE----------
                        /*-------------------*/
                        
                        DROP TABLE ##LOCK_SECTION_A
                        BREAK;        
            END TRY                                        
            BEGIN CATCH
            
                  IF (OBJECT_ID('tempdb..##LOCK_SECTION_A') IS NOT NULL)
                        DROP TABLE ##LOCK_SECTION_A
                           
            END CATCH  
      END              
      
      WAITFOR DELAY '00:00:00:100'
END

History

  • 23rd October, 2015: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Argentina Argentina
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseSimply but usefull! Pin
alex_perea21-May-20 13:40
alex_perea21-May-20 13:40 
SuggestionAlternative ways to have "critical section" and why. Pin
Maurice code project26-Oct-15 16:26
Maurice code project26-Oct-15 16:26 
GeneralRe: Alternative ways to have "critical section" and why. Pin
Hernán Hegykozi26-Oct-15 17:16
Hernán Hegykozi26-Oct-15 17:16 
QuestionWhy? Pin
Tomas Takac24-Oct-15 0:45
Tomas Takac24-Oct-15 0:45 
AnswerRe: Why? Pin
Hernán Hegykozi24-Oct-15 19:26
Hernán Hegykozi24-Oct-15 19:26 
GeneralRe: Why? Pin
Tomas Takac25-Oct-15 23:40
Tomas Takac25-Oct-15 23:40 
GeneralRe: Why? Pin
Hernán Hegykozi26-Oct-15 17:13
Hernán Hegykozi26-Oct-15 17:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.