使用事务时的数据库死锁


public async Task<T> Method1(){      
    using (var transaction = Conn.GetTransaction())
            {
                await Conn.DeleteMany<M1>().Where(x => (x.d == dId).ExecuteAsync();
                foreach (var l in listData)
                {
                    await Conn.InsertAsync(l);
                }
                transaction.Complete();
            }
    }

public async Task<T> callerMethod(){
   var res = await serivce.Method1();
}
for(int i = 0; i <25; i++){
    callerMethod();
}

当我通过 web api 调用它时 [通过循环调用它> 25 次]。 插入一些记录,一些收到"死锁"消息。

当我删除"using(事务("块时,它工作正常。但是,我相信我们需要有"使用(交易(块。

您的问题是混合访问数据库,有时通过事务,有时通过事务外部。这会导致锁定问题 - 无法从事务外部再次锁定事务时锁定在事务内部的资源。这就是僵局。

由于你想要一个事务,我会切换到一个TransactionScope,它应该通过异步调用进行。

最新更新