使用事务

时间:2018-01-31 21:12:54

标签: asp.net entity-framework async-await npoco

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调用它时[通过循环调用它&gt; 25次]。一些记录被插入,一些记录得到了#34;死锁&#34;信息。

当我删除&#34;使用(交易)&#34;阻止它工作正常。但是,我认为我们需要使用(事务)块。

1 个答案:

答案 0 :(得分:-1)

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

由于你想要一个交易,我会转而使用TransactionScope,它应该在异步调用中进行。

相关问题