简体   繁体   中英

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.

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.

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. 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).

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. No process will be able to access the tables you have touched (even reading from it) during your transaction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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