简体   繁体   English

ASP.NET MVC / C#/ SQL Server应用程序:多个用户同时更新表的问题

[英]ASP.NET MVC / C# / SQL Server Application : Issue with multiple users updating a table same time

I am working on an ASP.NET MVC/C#/ SQL Server application and I have a transaction to update multiple tables. 我正在开发ASP.NET MVC / C#/ SQL Server应用程序,我有一个事务来更新多个表。

using (var scope = TransactionHelpers.CreateTransactionScopeRequiresNew())
{
      // CRUD operations for table 1, table 2....
}

It works fine when a single user call above method. 当单个用户调用上面的方法时,它工作正常。 However, if 2 users call above method almost simultaneously then one request works and other fails, probably due to table locks by previous transaction. 但是,如果2个用户几乎同时调用上述方法,则一个请求起作用而另一个请求失败,这可能是由于先前事务的表锁定。

So what is the best work around for this issue ? 那么这个问题的最佳解决方案是什么?

The default isolation mode is read committed and fits perfectly to 99% of your needs, eg. 默认隔离模式是read committed ,完全符合99%的需求,例如。 reading data. 读数据。 When you want to save the changes you made to the database (Create, Update, Delete), Entity Framework is smart enough to create a transaction without your notice behind the scenes to wrap the changes. 当您想要保存对数据库所做的更改(创建,更新,删除)时,实体框架足够智能,可以在幕后创建事务而不需要您通知来包装更改。 You can be sure that everything will be saved or every change will be discarded (Atomic). 您可以确保一切都将被保存,或者每个更改都将被丢弃(Atomic)。

By using transactions in Entity Framework, you change that behavior and force every CRUD operation during a transaction scope to be executed in serializable isolation mode which is the highest and most blocking type. 通过在Entity Framework中使用事务,您可以更改该行为并强制在事务范围内的每个CRUD操作以可serializable隔离模式执行,这是最高和最阻塞的类型。 No process will be able to access the tables you have touched (even reading from it) during your transaction. 在您的交易过程中,任何流程都无法访问您触摸的表格(甚至可以从中读取)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM