1
Answer

LINQ to SQL Thread safety

Ask a question
Antonis P

Antonis P

11y
1.4k
1
Hi,

I want to ask whether the following code is thread safe:
Consider that Save1 and Save2 will be executed concurrently. Is there any problem with the thread safety of the datacontext?

public class Test1()
{
        private void Save1()
        {        
                  using(TestLinqToSQL obj = new TestLinqToSQL())
                  {        
                      obj.SaveRecord(new Client (){Id = 1, Name = "John Doe");
                  }
        }

        private void Save2()
        {        
                  using(TestLinqToSQL obj = new TestLinqToSQL())
                  {        
                      obj.SaveRecord(new Client (){Id = 2, Name = "Mike Tyson");
                  }
        }         
}



public class TestLinqToSQL : IDisposable
{
        public void SaveRecord(Client newClient)
        {
                using(ClientDatacontext cont = new ClientDatacontext())
                {
                        cont.InsertRecord(newClient);
                }        
        }
}

Thanks in advance



Answers (1)