FreeSql icon indicating copy to clipboard operation
FreeSql copied to clipboard

IBaseRepository.UpdateAsync()方法批量更新概率性报错

Open Naughtyhusky opened this issue 1 year ago • 1 comments

问题描述及重现代码:

  1. 该方法为定时任务调度,一个小时执行一次。 应该不存在多线程问题。
  2. 执行逻辑是循环每次获取100条数据,修改数据之后调用UpdateAsync()方法批量更新
  3. 报错为概率性,不是必现,但是概率较大。
  4. 报错信息如下: ArgumentOutOfRangeException: Index must be within the bounds of the List. (Parameter 'index') at System.Collections.Generic.List1.InsertRange(System.Int32 index, System.Collections.Generic.IEnumerable1 collection) at offset 268 at FreeSql.DbSet`1.<DbContextBatchUpdatePrivAsync>d__65.MoveNext() at offset 944 at FreeSql.<>c__DisplayClass62_0.<<FlushCommandAsync>g__funcUpdate|3>d.MoveNext() at offset 269 at FreeSql.DbContext.<FlushCommandAsync>d__62.MoveNext() at offset 1337 at FreeSql.RepositoryDbContext.<SaveChangesAsync>d__8.MoveNext() at offset 111
  while(true)
{
    var list = _repository.where(...).take(100).ToListAsync();
    ......
    await _repository.UpdateAsync(list);
   await Task.Delay(100);
}

数据库版本

Mysql 8.0

安装的Nuget包

3.2.8

.net framework/. net core? 及具体版本

.net 6.0

Naughtyhusky avatar Jan 15 '24 08:01 Naughtyhusky

控制不要多线程访问 _repository 这个对象

2881099 avatar Jan 15 '24 14:01 2881099