Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7770951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:31:50+00:00 2026-06-01T16:31:50+00:00

I am maintaining a sproc where the developer has implemented his own locking mechanism

  • 0

I am maintaining a sproc where the developer has implemented his own locking mechanism but to me it seemed flawed:

CREATE PROCEDURE Sproc 1
AS

Update X
set flag = lockedforprocessing
where flag = unprocessed

-- Some processing occurs here with enough time to 
-- 1. table X gets inserted new rows with a flag of unprocessed
-- 2. start another instance of this Sproc 1 that executes the above update

Select from X
where flag = lockedforprocessing

-- Now the above statement reads rows that it hadn't put a lock on to start with.

I know that I can just wrap it sproc inside a transaction with isolation level of SERIALIZABLE but I want to avoid this.

The goal is

  1. that multiple instances of this sproc can run at the same time and process their own “share” of the records to achieve maximum concurrency.
  2. An execution of the sproc should not wait on a previous run that is still executing

I don’t think REPEATABLE READ can help here since it won’t prevent the new records with a value of “unprocessed” being read (correct me if I’m wrong please).

I just discovered the sp_getlock sproc and it would resolve the bug but serialize exaction which is not my goal.

A solution that I see is to have each run of the proc generate its own unique GUID and assign that to the flag but somehow I am thinking I am simulating something that SQL Server already can solve out of the box.

Is the only way that let each run of a sproc process it’s “share” of the rows to have it in SERIALIZABLE?

Regards, Tom

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:31:51+00:00Added an answer on June 1, 2026 at 4:31 pm

    Assuming there is an ID field in X, a temporary table of updated Xs can help:

    CREATE PROCEDURE Sproc 1
    AS
    -- Temporary table listing all accessed Xs
       declare @flagged table (ID int primary key)
    -- Lock and retrieve locked records
       Update X
          set flag = lockedforprocessing
       output Inserted.ID into @flagged
        where flag = unprocessed
    
    -- Processing
       Select from X inner join @flagged f on x.ID = f.ID
    
    -- Clean-up
       update X
          set flag = processed
         from x inner join @flagged f on x.ID = f.ID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm maintaining a software developed using J2SE, (but i'm c# developer actually not have
I assist in maintaining an enterprise web-based system (programmed in J2EE, but this is
I am maintaining an existing BlackBerry application (implemented as a MIDlet). The application contains
I'm maintaining a website that has full screen background photos, and a text panel
I'm currently maintaining an old plugin for Office. Current solution has a method for
I'm maintaining a ActionScript 2 application which uses createEmptyMovieClip to create dropdown lists (the
The site I'm maintaining has been designed pretty rigidly using pixels for font sizes,
I am working on maintaining a ASP.NET MVC application that has the following coding
I am maintaining a Windows Service, which has a quite unfortunate bug. I can
I am maintaining a VS2010 project which has a number of cross-referenced COM libraries.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.