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

  • SEARCH
  • Home
  • 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 961619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:22:29+00:00 2026-05-16T01:22:29+00:00

I have many .NET processes reading messages from an SQL Server 2008 DB table

  • 0

I have many .NET processes reading messages from an SQL Server 2008 DB table and processing them one at a time. I implement a simple SP to ‘lock’ the row that is being read by any one process, to avoid any two processes processing the same row.

BEGIN TRAN

SELECT @status = status FROM t WHERE t.id = @id
IF @status = 'L'
BEGIN
    -- Already locked by another process. Skip this
    RETURN 0
END
ELSE
    UPDATE t SET status = 'L' WHERE id = @id
    RETURN 1
END

COMMIT

However, this is flawed: sometimes a row gets ‘locked’ and processed twice. I suspect there is a concurrency problem: two processes reading the status before one updates it.

I think this could be resolved by implementing a read block somehow (i.e make the transaction block READs), but I am unsure how to do this. Can anyone help?

Thanks a lot in advance

Ryan

  • 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-05-16T01:22:30+00:00Added an answer on May 16, 2026 at 1:22 am

    Did you try using:

    SELECT @status = status FROM t (UPDLOCK) WHERE t.id = @id
    

    Refer to this link for more details.

    The thing you’re missing is that a SELECT statement doesn’t normally lock the row, so if one process has executed the SELECT but hasn’t execute the UPDATE yet, but then another process comes along and executes the SELECT, then it’s going to return the row back since you haven’t locked it.

    By using the UPDLOCK, you lock that row with your SELECT statement and prevent the other process from getting it back until the first process commits the transaction, which is the behavior you want.

    EDIT
    Of course, doing it with one statement as Martin suggests is the easiest way and you avoid having to deal with the locking issue at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im using c# .net windows form application. I have many databases created using sql
I have too many images to be download from net into iPhone? How can
I have an ASP.NET 2.0 page with many repeating blocks, including a third-party server-side
I have an ASP.NET MVC web site. I have many actions which require authentication
I'm new in ASP.NET MVC, I have many actions in my controllers, so they
I use webforms/masterpage, with .NET. I Have many Web User Controls into my page.
I have been looking at many ASP.Net MVC client side validation ideas including xVal.
Im using c# .net windows application form. I have created many databases with many
I have read so many times, here and everywhere on the net, that mutexes
I have created a great stand-alone web form in asp.net utilizing many jQuery features

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.