SqlConnection with concurrent programming

This is my existing code that stores some data in multiple tables.

using (SqlConnection conn = new SqlConnection("myConnString"))
{
   DoWork1(conn);
   DoWork2(conc);
   DoWork3(conn);
}

To speed up my code, I am trying to get .net TPL support, and I modified my code below

using (SqlConnection conn = new SqlConnection("myConnString"))
{
   ParallelOptions pw = new ParallelOptions();
   pw.MaxDegreeOfParallelism = Environment.ProcessorCount;

   Parallel.Invoke(pw,()=> DoWork1(conn),()=> DoWork2(conc),()=> DoWork3(conn));
} 

But that throws me an exception of a fatal internal connection error from the ExecuteNonQuery () method in my data access level. Is my parallel approach wrong?

+5
source share
1 answer

Well, there are ways you could work using MARS - but I would suggest a different approach. (I don't know if MARS supports the same connection across multiple threads, although it does allow multiple simultaneous operations.)

, , ( ) . .NET, , parallelism : , , .

+8

All Articles