提问人:Firman Alam 提问时间:11/14/2023 最后编辑:Firman Alam 更新时间:11/15/2023 访问量:52
.NET Core 使用者 Kafka 错误死锁 SQL Server
.NET Core Consumer Kafka error deadlock SQL Server
问:
我是 .NET 的新手。我有使用 .NET Core 的微服务。此服务用于成为 Kafka 使用者。在使用者函数中,有一个要插入到 SQL Server 数据库中的函数。此函数经常遇到错误,但有时当流量较低时,此代码不会出错。我附上了函数代码。这个函数很简单,只需使用事务插入数据库即可。如果有人经历过这样的事情,请提供帮助。错误如下所示。deadlock
set identity insert on
An exception occurred in the database while saving changes for context type 'Entities.RepositoryContext'.
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
---> Microsoft.Data.SqlClient.SqlException (0x80131904): Transaction (Process ID 414) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
private async Task insertData(Dto data)
{
var dataFromDB = _repository.myRepo.FirstOrDefaultWithNoLock(p => p.ID.Equals(data.ID));
if (dataFromDB == null)
{
dataFromDB = _mapper.Map<Model>(data);
_repository.myRepo.Create(dataFromDB);
_repository.myRepo.SaveChangesWithIdentityInsert();
return;
}
return;
}
我在里面附上了代码SaveChangesWithIdentityInsert
public static void EnableIdentityInsert<T>(this DbContext context) => SetIdentityInsert<T>(context, true);
public static void DisableIdentityInsert<T>(this DbContext context) => SetIdentityInsert<T>(context, false);
private static void SetIdentityInsert<T>([NotNull] DbContext context, bool enable)
{
if (context == null) throw new ArgumentNullException(nameof(context));
var entityType = context.Model.FindEntityType(typeof(T));
var value = enable ? "ON" : "OFF";
context.Database.ExecuteSqlRaw($"SET IDENTITY_INSERT dbo.{entityType.GetTableName()} {value}");
}
public static void SaveChangesWithIdentityInsert<T>([NotNull] this DbContext context)
{
if (context == null) throw new ArgumentNullException(nameof(context));
var strategy = context.Database.CreateExecutionStrategy();
strategy.Execute(
() =>
{
using var transaction = context.Database.BeginTransaction(isolationLevel: System.Data.IsolationLevel.ReadUncommitted);
context.EnableIdentityInsert<T>();
context.SaveChanges();
context.DisableIdentityInsert<T>();
transaction.Commit();
});
context.ChangeTracker.Clear();
}
我尝试使用.我正在搜索在发生死锁时自动重试消费者,但没有找到任何东西。with no lock
答: 暂无答案
评论
SaveChangesWithIdentityInsert()
WITH (NOLOCK)
SaveChangesWithIdentityInsert
SaveChanges
FirstOrDefaultWithNoLock
也毫无意义,因为 DbContext 不是数据库连接。除非它必须读取或写入数据,否则它甚至不会打开连接。