Reputation: 193
I am having an issue when executing multiple threads using TPL Parallel.ForEach() method in c#. The multi-processing accesses the database, and I also wrap the each action with a using statements so that it disposes the connection after each execution.
The error I get says: The query processor could not start the necessary thread resources for parallel query execution
After a lot of googling, SQL is out of resources due to it being too busy and or causing some memory leaks.
I have also tried setting the ParallelOptions parameter in the Parallel.ForEach() and setting the MaxDegreeOfParallelism = 2 but this does not help. i.e.
Parallel.ForEach(customerNumbers, cno =>
{
using (var ctx = new MyContext())
{
// do database call
}
}, new ParallelOptions { MaxDegreeOfParallelism = 2 });
Can anyone provide me with a light on how to fix this?
Upvotes: 2
Views: 5250
Reputation: 3277
The problem is not on C#'s side, it's with your SQL Server. Your SQL server is rejecting multiple simultaneous calls.
Please make sure:
Upvotes: 3