Protect Stored Procedures Against Multiple Concurrent Executions

Project Management

Stored procedures are a great way to encapsulate business logic in your database, but we don’t always want the stored procedure to be executed by more than one user at the exact same time. In this article by Hans Michiels, we see how he creates the logic to prevent concurrent execution.

How it works

  • You create the table [dbo].[udm_storedproc_executions] (or name it differently) in your database.
  • You create the stored procedure [sp].[GetPace] (or name it differently) in your database.
  • You add the code snippet below to stored procedures that are not supposed to run multiple times concurrently.

Proof that it works

For the demo I use the following stored procedures:

  • [sp].[SubStoredProc]
  • [sp].[StoredProcGettingPace]
  • [sp].[StoredProcGettingPaceNoWait]

[sp].[SubStoredProc] is being executed by both other stored procedures.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s