I have a server that receives and processes messages. It then calls a method that uses LINQ To SQL to add the message in the database. As messages come in, they are set onto separate threads for processing, so it is possible that I may get two threads calling my SQL method at the same time.
I am curious whether LINQ to SQL will create separate data contexts for each thread calling the method, or if the context is static. I'm also wondering if I should put a threadlock before calling SubmitChanges().
I think the question is pretty straightforward, but I will add a code snippet just in case it helps.
using (MyDataContext db = new MyDataContext(args.ConnectionText))
{
// Remove records with matching iprocid and atype
IQueryable<densgrd> rows = from grades in db.densgrds
where grades.iprocid == args.DensGrd.iprocid &&
grades.atype == args.DensGrd.atype
select grades;
// Remove any results from the database.
foreach (densgrd remove in rows)
{
db.densgrds.DeleteOnSubmit(remove);
}
db.densgrds.InsertOnSubmit(args.DensGrd);
db.SubmitChanges();
}