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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:36:33+00:00 2026-05-16T10:36:33+00:00

My code is written with C# and the data layer is using LINQ to

  • 0

My code is written with C# and the data layer is using LINQ to SQL that fill/load detached object classes.

I have recently changed the code to work with multi threads and i’m pretty sure my DAL isn’t thread safe.

Can you tell me if PopCall() and Count() are thread safe and if not how do i fix them?

public class DAL
{
   //read one Call item from database and delete same item from database. 
    public static OCall PopCall()
    {
        using (var db = new MyDataContext())
        {
            var fc = (from c in db.Calls where c.Called == false select c).FirstOrDefault();
            OCall call = FillOCall(fc);
            if (fc != null)
            {
                db.Calls.DeleteOnSubmit(fc);
                db.SubmitChanges();
            }
            return call;
        }
    }

    public static int Count()
    {
        using (var db = new MyDataContext())
        {
            return (from c in db.Calls select c.ID).Count();
        }
    }

    private static OCall FillOCall(Model.Call c)
    {
        if (c != null)
            return new OCall { ID = c.ID, Caller = c.Caller, Called = c.Called };
        else return null;
    }
}

Detached OCall class:

public class OCall
{
    public int ID { get; set; }
    public string Caller { get; set; }
    public bool Called { get; set; }
}
  • 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-16T10:36:33+00:00Added an answer on May 16, 2026 at 10:36 am

    Individually they are thread-safe, as they use isolated data-contexts etc. However, they are not an atomic unit. So it is not safe to check the count is > 0 and then assume that there is still something there to pop. Any other thread could be mutating the database.

    If you need something like this, you can wrap in a TransactionScope which will give you (by default) the serializable isolation level:

    using(var tran = new TransactionScope()) {
        int count = OCall.Count();
        if(count > 0) {
            var call = Count.PopCall();
            // TODO: something will call, assuming it is non-null
        }
    }
    

    Of course, this introduces blocking. It is better to simply check the FirstOrDefault().

    Note that PopCall could still throw exceptions – if another thread/process deletes the data between you obtaining it and calling SubmitChanges. The good thing about it throwing here is that you shouldn’t find that you return the same record twice.

    The SubmitChanges is transactional, but the reads aren’t, unless spanned by a transaction-scope or similar. To make PopCall atomic without throwing:

    public static OCall PopCall()
    {
        using(var tran = new TrasactionScope())
            using (var db = new MyDataContext())
            {
                var fc = (from c in db.Calls where c.Called == false select c).FirstOrDefault();
    
                OCall call = FillOCall(fc);
    
                if (fc != null)
                {
                    db.Calls.DeleteOnSubmit(fc);
                    db.SubmitChanges();
                }
    
                return call;
            }
            tran.Complete();
        }
    }
    

    Now the FirstOrDefault is covered by the serializable isolation-level, so doing the read will take a lock on the data. It would be even better if we could explicitly issue an UPDLOCK here, but LINQ-to-SQL doesn’t offer this.

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

Sidebar

Related Questions

I have written a code to insert the data to SQL LITE database as
I have written a code for taking JSON data from a PHP and putting
I have written java code for generating json of my searched data from file.But
I've recently written a piece of code to read some data from a file,
I have written a simple code in page test_cookie.php to work with cookies. if(isset($_GET['data']))
I have written a working T-SQL MERGE statement. The premise is that Database A
We are using EF 4.3 for our data layer and have a generic repository
Has anybody ever successfully written code to extract data from a SharePoint 2007 list
I've written the following code to display some data, but the data grid is
I have following code written to run NSTimer . But the NSTimer selector is

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.