SqlConnection hangs in the open

I have a program that sends multiple queries in parallel to an instance of SQL Server. Of all my simultaneous requests, one of them always blocks for several seconds on db.open ().

void MyMethod()
{
    var brands = new  List<string>{"Chevy", "Honda", "Ford", "GM"};
    var foundCars = new  ConcurrentBag<Car>();   

    Parallel.ForEach(brands, brand =>
    {
      logger.Trace(brand + " before enqueue");
      foundsCars.Enqueue(FindCar(brand));
      logger.Trace(brand + " after enqueue");
    });
}

public Car FindCars(string brand)
{
   using (var db = new SqlConnection(connectionString))
   {
     sqlLogger.Trace("Brand " + " brand " + " before db open");
     db.Open();
     sqlLogger.Trace("Brand " + " brand " + " after db open");
     using (var cmd = new SqlCommand(sqlCmd, db))
     {
        while (reader.Read())
        {              
            //SQL SELECT Stuff going on
        }
     }
   }
}

If I looked in my footprint, I would see something like this, the order depends on the db response time, which I assume in this Honda scenario is data that is slowly processed, but it will not always be:

12:00:00.0000 Chevy before enqueue 
12:00:00.0000 Honda before enqueue 
12:00:00.0000 Ford before enqueue
12:00:00.0000 GM before enqueue
12:00:00.0200 Brand Chevy before db open
12:00:00.0300 Brand Honda before db open
12:00:00.0200 Brand Ford before db open
12:00:00.0200 Brand GM before db open
12:00:00.0300 Brand Chevy after db open
12:00:00.0300 Brand Ford after db open
12:00:00.0300 Brand GM after db open
12:00:00.0400 Chevy after enqueue
12:00:00.0400 Ford after enqueue
12:00:00.0400 GM after enqueue
12:00:07.0000 Brand Honda after db open <-- usually around 7 seconds late
12:00:07.0100 Honda after enqueue

I cannot reproduce this problem locally on my instance of SQL Server 2008, but this happens all the time on the instance of SQL Server 2012. The problem seems to be a configuration problem. This always happens when I send parallel queries and regardless of the SELECT query. If I made a blocking query in SQL Server Management, it would return to sub 100ms.

:

<add key="SqlConnectionString" value="Data Source=10.0.20.20;Initial Catalog=CarsDB;Integrated Security=SSPI;Connection Timeout=240;" />

Edit:

"Min Pool Size = 30" , , , , , +15 , - 500 .

SQL Server 2012, environement , , , . , .

+4
4

,

Parallel.ForEach(brands, brand =>
{
  logger.Trace(brand + " before enqueue");
  foundsCars.Enqueue(FindCar(brand));
  logger.Trace(brand + " after enqueue");
});

, , , . , . , , ? . .

+1

MARS ConnectionString. : Data Source=10.0.20.20;Initial Catalog=CarsDB;Integrated Security=SSPI;Connection Timeout=240;MultipleActiveResultSets=True;

0

. Parallel.ForEach . , IO. parallelism .

0

SSPI / . , Active Directory, - .

0

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


All Articles