您的位置:首页 > 产品设计 > UI/UE

Handle concurrency problem by Entity Framework

2013-09-11 20:56 369 查看
Database concurrency

A concurrency conflict occurs when multiple users try to change the same data simultaneously. For example, if user one and user two load the same product from database, the user who clicks submit
button last will be the one who wins, and the user's data will override the first user's data without any warning being given to either user.

How to handle the concurrency problem?

A. Pessimistic concurrency. This approach uses the database row lock. Obviously, this approach has performance problem.

B. Optimistic concurrency. This approach verifies that the data to be updated has not been modified after retrieving. Entity framework supports this approach.

With entity framework, you need to define a timestamp column in the database table, then you need to put a [Timestamp] attribute for the column in the entity model. Then when you try to update a table,
entity framework will check if the data has been changed by comparing the time stamp filed, if it has been changed , then entity framework will throw a exception of OptimisticConcurrencyException.

But Timestamp data type of SQL Server will be removed in the future, one new equivalent data type rowversion should be used.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息