Parallel.ForEach and DbContext

本文关键字:DbContext and ForEach Parallel | 更新日期: 2023-09-27 17:59:07

我正在使用Parallel.ForEach,它极大地提高了我的代码的性能,但我对具有多线程的DbContext很好奇。我知道它不是线程安全的,所以我在需要的地方使用锁。

循环遍历字典并计算统计信息:

Dictionary<string, List<decimal>> decimalStats = new Dictionary<string, List<decimal>>(); // this gets populated in another irrelevant loop
List<ComparativeStatistic> comparativeStats = db.ComparativeStatistics.ToList();
var statLock = new object();
Parallel.ForEach(decimalStats, entry =>
{
    List<decimal> vals = ((List<decimal>)entry.Value).ToList();
    if (vals.Count > 0)
    {
        string[] ids = entry.Key.Split('#');
        int questionId = int.Parse(ids[0]);
        int yearId = int.Parse(ids[1]);
        int adjacentYearId = int.Parse(ids[2]);
        var stat = comparativeStats.Where(l => l.QuestionID == questionId && l.YearID == yearId && l.AdjacentYearID == adjacentYearId).FirstOrDefault();
        if (stat == null)
        {
            stat = new ComparativeStatistic();
            stat.QuestionnaireQuestionID = questionId;
            stat.FinancialYearID = yearId;
            stat.AdjacentFinancialYearID = adjacentYearId;
            stat.CurrencyID = currencyId;
            stat.IndustryID = industryId;
            lock (statLock) { db.ComparativeStatistics.Add(stat); }
        }
        stat.TimeStamp = DateTime.Now;
        decimal total = 0M;
        decimal? mean = null;
        foreach (var val in vals)
        {
            total += val;
        }
        mean = Decimal.Round((total / vals.Count), 2, MidpointRounding.AwayFromZero);
        stat.Mean = mean;
    }
});
db.SaveChanges();

我的问题:为什么我在向数据库添加内容时只需要锁?如果stat从不为null——如果它总是有一个数据库条目——我可以在没有锁的情况下运行这个循环,并且数据库会按预期更新。如果某个循环的stat为null,而我在那里没有锁,那么就会抛出一个System.AggregateException

edit1:我尝试每次打开一个到数据库的新连接,而不是使用lock,这在添加到数据库时也有效(与上面的循环相同,我在不同的地方添加了注释):

Parallel.ForEach(decimalStats, entry =>
{
    List<decimal> vals = ((List<decimal>)entry.Value).ToList();
    if (vals.Count > 0)
    {
        using (var dbThread = new PDBContext()) // new db connection
        {
            string[] ids = entry.Key.Split('#');
            int questionId = int.Parse(ids[0]);
            int yearId = int.Parse(ids[1]);
            int adjacentYearId = int.Parse(ids[2]);
            var stat = comparativeStats.Where(l => l.QuestionID == questionId && l.YearID == yearId && l.AdjacentYearID == adjacentYearId).FirstOrDefault();
            if (stat == null)
            {
                stat = new ComparativeStatistic();
                stat.QuestionnaireQuestionID = questionId;
                stat.FinancialYearID = yearId;
                stat.AdjacentFinancialYearID = adjacentYearId;
                stat.CurrencyID = currencyId;
                stat.IndustryID = industryId;
                dbThread.ComparativeStatistics.Add(stat); // no need for a lock
            }
            stat.TimeStamp = DateTime.Now;
            decimal total = 0M;
            decimal? mean = null;
            foreach (var val in vals)
            {
                total += val;
            }
            mean = Decimal.Round((total / vals.Count), 2, MidpointRounding.AwayFromZero);
            stat.Mean = mean;
            dbThread.SaveChanges(); // save
        }
    }
});

这样做安全吗?我确信实体框架的连接池足够智能,但我想知道是否应该添加任何参数来限制线程/连接的数量。

Parallel.ForEach and DbContext

几年后。你是对的。正确的解决方案是在Parallel.ForEach循环中创建一个DbContext的新实例。DbContext的创建是轻量级的,但上下文不是线程安全的。话虽如此,将上下文的生存期限制在最短的必要持续时间内真的很重要。因此,请不要让它在请求后长时间打开(ASP.NET)。处理DbContexts时最好的模式是using语句。

有几个可用的文档:

https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/

https://learn.microsoft.com/en-us/ef/ef6/fundamentals/working-with-dbcontext