LINQ to SQL DAL, is this thread safe?

My code is written in C #, and the data layer uses LINQ to SQL, which fills / loads individual classes of objects.

I recently changed the code to work with multiple threads, and I'm sure my DAL is not thread safe.

Can you tell me if PopCall () and Count () are thread safe, and if not, how can 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;
    }
}

Separate class OCall:

public class OCall
{
    public int ID { get; set; }
    public string Caller { get; set; }
    public bool Called { get; set; }
}
+3
source share
3 answers

, .. . , , count > 0, , - -. .

- , TransactionScope, ( ) :

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
    }
}

, . FirstOrDefault().

, PopCall - / SubmitChanges. , , , , .

SubmitChanges , , . PopCall :

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();
    }
}

FirstOrDefault , . , UPDLOCK , LINQ-to-SQL .

+3

Count() . , . , ? , .

PopCall, , . fc, , SubmitChanges(), , . , , .

+1

, Linq-To-Sql, SqlClient, System.Transactions PopCall() , "" " " (.. concurrency , /). #. , , doen. ( ), , , , .

, . SQL Server, OUTPUT. , , . - :

WITH cte AS (
 SELECT TOP(1) ... 
 FROM Calls WITH (READPAST)
 WHERE Called = 0)
DELETE
FROM cte
OUTPUT DELETED.*;

, Calls Called. , , .

In this context, the challenge Countis mostly useless. The only way to correctly validate an available element is Pop, asking Countjust to collect a useless effort in the database to return a value COUNT()that means nothing in a parallel environment.

+1
source

Source: https://habr.com/ru/post/1761491/


All Articles