Database connection pooling with multi-threaded service

前端 未结 3 366
甜味超标
甜味超标 2021-01-12 01:23

I have a .NET 4 C# service that is using the TPL libraries for threading. We recently switched it to also use connection pooling, since one connection was becoming a bottle

3条回答
  •  一向
    一向 (楼主)
    2021-01-12 01:42

    You could look to control the degree of parallelism by using the Parallel.ForEach() method as follows:

    var items = ; // your collection of work items
    var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = 100 };
    Parallel.ForEach(items, parallelOptions, ProcessItem)
    

    In this case I chose to set the degree to 100, but you can choose a value that makes sense for your current connection pool implementation.

    This solution of course assumes that you have a collection of work items up front. If, however, you're creating new Tasks through some external mechanism such as incoming web requests the exception is actually a good thing. At that point I would suggest that you make use of concurrent Queue data structure where you can place the work items and pop them off as worker threads become available.

提交回复
热议问题