title: Is LINQ To SQL Thread Safe?
body: <p>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. </p>
  
  <p>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().</p>
  
  <p>I think the question is pretty straightforward, but I will add a code snippet just in case it helps.</p>
  
  <pre><code>using (MyDataContext db = new MyDataContext(args.ConnectionText))
              {
                  //  Remove records with matching iprocid and atype
                  IQueryable&lt;densgrd&gt; rows = from grades in db.densgrds
                                             where grades.iprocid == args.DensGrd.iprocid &amp;&amp;
                                                   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();
              }
  </code></pre>