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 8319717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:24:32+00:00 2026-06-08T22:24:32+00:00

Avoiding duplicates on concurrent reads off the same database table We have a table

  • 0

Avoiding duplicates on concurrent reads off the same database table

We have a table that contains a list of tasks

Table RecordsTable  
    RecordID
    RecordName
    ...
    ...
    IsProcessed

Multiple worker machines read off the table and once the task is processed mark IsProcessed as true.

So if we want the following code to work without duplicates

Pseudocode in C#

//get first 10 records that are not processed based on some other conditions
var recordSet = objectontext.recordstable.Where(...).Where(c => c.IsProcessed == false).Take(10);
//loop through the recordset in a transaction 
foreach(record singleRecord in recordSet)
{
    bool result = ProcessRecord();
    //Mark isProcessed as true 
    if(result)
        singleRecord.IsProcessed = true;
    objectContext.Savechanges();
}

We want to avoid duplicate processing of records (since the ProcessRecords() contain mailers and such). If we wrap the entire code above in
a transaction
does it mean that two calls from two different workers would result in non-duplicate records?

If workerA first issues the call to the table it gets,

var recordSetWorkerA = objectontext.recordstable.Where(somecondition...).Where(c => c.IsProcessed == false).Take(10);

If workerB issues a call after worker A already is in the transaction would the following statment fail to execute because trying to read locked rows
or move to the next 10 records?

var recordSetWorkerB = objectontext.recordstable.Where(somecondition...).Where(c => c.IsProcessed == false).Take(10);

Is there any pattern we should be looking at.

  • 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-08T22:24:35+00:00Added an answer on June 8, 2026 at 10:24 pm

    Just wrapping your code into transaction won’t be enough. You’ll of course get exception on SaveChanges, but it’ll be too late.

    What you really need is marking records as being processed, not just completed processing. I see two solutions:

    1. If you workers share same state (meaning they are thread in one AppDomain, and not several concurrent worker services), you can use ConcurrentDictionary to mark records you’re being processed.

      foreach(record singleRecord in recordSet)
      {
          //RecordsInProcess is a globally-available ConcurrentDictionary<recordIdType, record
          if (!RecordsInProcess.TryAdd(singleRecord.RecordId, singleRecord))
             continue; //TryAdd will return false if such an element already exists
      
          bool result = ProcessRecord();
          //Mark isProcessed as true 
          if(result)
              singleRecord.IsProcessed = true;
          objectContext.Savechanges();
          record junk; // we don't need it
          RecordsInProcess.TryRemove(singleRecordId, out junk)
      }
      
    2. If you workers are isolated or you just want something more robust, then you have to mark records as processing in database and use that info for filtering. That’s where you have to use transactions, and use them very carefully, because it’s very easy to get deadlocked. Most efficient from concurrency point of view will be to always take only one unprocessed record from database, mark it as processing before you do anything and then continue with your processing.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Avoiding repeated constants in CSS I have a javascript file that I
I have been reading the book SQL Antipatterns: Avoiding the Pitfalls of Database Programming
Possible Duplicate: Avoiding repeated constants in CSS We have some theme colors that are
Thinking about avoiding code replication, I got a question that catches me every time
I am avoiding the creation of files on disk, this is what I have
just wondering what tips people have for avoiding game synchronisation issues in multiplayer games
Avoiding Fat Controller So I'm using Zend Framework and I have a question involving
Hi I have a database with loads of columns and I want to insert
I know the solid security recommendation of avoiding accepting user input that you then
I am having trouble avoiding a sporadic crash in my applet that appears to

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.