简体   繁体   中英

ACID concurrency ISOLATION LEVEL vs ROWVERSION

I'm testing concurrency on sql server 2012 and using entity framework.

I've noticed that concurrency can be mainly controlled in one of these ways:

  1. SET TRANSACTION ISOLATION LEVEL [REPEATABLE READ|SERIALIZABLE|...]
  2. Creating a ROWVERSION column on the table

From what I understand, the first way uses database logs and locks, while the second uses a basic comparison of the Rowversion value.

Please correct me if what I'm saying is wrong.

What is the best choice? Should I use them both or just stick to one?

Typically, each dbcontext saveChanges() is enclosed in a database transaction . We are talking about to handle concurrency at different levels: into a database transaction on saveChanges and between several dbcontext saveChanges:

  • Database layer : Database is able to handle isolation for live transactions. You can change isolation level with set transaction isolation level command.

  • Entity framework layer : EF is able to keep concurrency from one transaction to another, just to know if other user has changed your data. They are two ways:

    • Configure the Entity Framework to include the original values of every column in the table in the Where clause of Update and Delete commands. As in the first option, if anything in the row has changed since the row was first read, the Where clause won't return a row to update, which the Entity Framework interprets as a concurrency conflict. For database tables that have many columns, this approach can result in very large Where clauses, and can require that you maintain large amounts of state.

    • In the database table, include a tracking column that can be used to determine when a row has been changed. You can then configure the Entity Framework to include that column in the Where clause of SQL Update or Delete commands. The data type of the tracking column is typically rowversion .

This is the trip for MVC:

db.Entry(departmentToUpdate).OriginalValues["RowVersion"] = rowVersion;

Quotes and detailed info: http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application

SQL Server maintains data integrity and system stability by adhering to the ACID properties which is an acronym for Atomicity, Consistency, Isolation and Durability.

The isolation part of this acronym states that all transactions are run in isolation from each other and no two resources can update the same data or definition of a SQL object at the same time. In the pessimistic isolation levels (Read Uncommitted, Read Committed, Repeatable Read and Serialize-able) this is controlled by locking. In the optimistic isolation levels (Read Committed Snapshot and Snapshot) this is controlled by row versioning.

Excerpted from https://simonlearningsqlserver.wordpress.com/2013/11/15/locking-blocking-and-isolation-levels/

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