Matteo Pietro Peru
Matteo Pietro Peru

Reputation: 576

How to determine the number of parallelism degrees in a ParallelForeachAsync combined with DbContext

after reading some docs on how to manage DB connections via DbContext (that isn't thread safe) inside a Parallel.ForEach, I was wondering if there was an intelligent way to manage the MaxDegreeOfParallelism property based on the webserver resources combined with the maximum pool size of the DbContext so as not to excessively stress the database having to iterate even thousands of records, or if eventually that `MaxDegreeOfParallelism is not the property that I have to manage for my case

This is my iteration:

public async Task<T> DoThings()
{
    HashSet<FruizioneDto> theList = // get items;
    
    DbContextOptionsBuilder<WebinarDbContext> builder = new();
    builder.UseLazyLoadingProxies();
    builder.UseSqlServer(Environment.GetEnvironmentVariable("ConnectionString"));
    PooledDbContextFactory<WebinarDbContext> factory = new(options: builder.Options, poolSize: 1024); // <- 1024 is the PooledDbContext default value

    ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = -1 }; // -1 = run at full speed
    await Parallel.ForEachAsync(theList, parallelOptions, async (fruizione, cancellationToken) =>
    {
        using WebinarDbContext context = factory.CreateDbContext();
        using (IDbContextTransaction transaction = context.Database.BeginTransaction())
        {
            try
            {
                // some business logic...
        
                transaction.Commit();
            }
            catch (FunctionBusinessLogicException ex)
            {
                transaction.Rollback();

                // some exception handling...

                return;
            }
            catch (Exception ex)
            {
                transaction.Rollback();

                // some exception handling...
                return;
            }
        }
    }

   return T;
}

What i do inside the loop is generate a pdf certificate about a course taken by a user and update some records to DB for each iteration. What I'm iterating (theList) is a collection of objects of type FruizioneDto containing information about the user and the course followed.

I calculate the valid "credits" for the course, I do some replaces on an html template which I convert into pdf and upload to an Azure storage account. If all these operations went well, I'm going to update the database on the Fruizione table, and I add a record on the newly generated certificate in another table. All this for each iteration. It's a lot of code but if needed I can share it.

Is there any way to determine how many degrees of parallelism combined with the max pool size of the DbContext? thanks!

Upvotes: 0

Views: 108

Answers (0)

Related Questions