Handle Race Conditions / Concurrency in Code First Entity Framework Applications

Preface

A common scenario in developing applications is the handling of concurrent database updates.

Given there is an application to manage customers, that is used by multiple users simultaneous.

User U1 reads the data of customer C1. While user U1 is looking at this data, user U2 reads the data of customer C1 too. Meanwhile, user U1’s phone is ringing. She picks it up. User U2 changes some of the customer’s data and saves it to the database. User U1 finishes the phone call, make some changes to the same customer, and saves it too.

Without handling the race condition or concurrency, al changes made by user U2 will be lost in this example. Handling it, the application should not save the changes made by user U1 and inform her that the customer’s data was changed in the meantime by another user.

In this post I will show an approach and sample code on how an application using the Entity Framework and the Code First approach can handle this.

Code Preparation

There is only one step required to make Entity Frame do all the work for you: Define a Version property in the model class (the name of the property does not matter at all), and decorate it with a Timestamp attribute.

/// <summary>
/// This column will be used by EF for race condition validation.
/// </summary>
[Timestamp]
public byte[] Version { get; set; }

Believe it or not – this is all you have to do (in case you are using the Code First approach).

Entity Framework will set the Version property to a value when the record is inserted into the database. Every time EF updates the record, it verifies that the value of the column has not changed. Doing an update, the version is increased.

Handling a Race Condition

EF throws a DbUpdateConcurrencyException in case a record was changed since the data was read.

So you have to catch this exception whenever your application updates or deletes database records and inform the user that the data was changed. In case there is no user to be informed, e.g. if this happens in a background process, you have to implement an appropriate error handling mechanism.

Using the Sample

To see EF Concurrency Handling in action, you can use the sample application.

There are two “clients” implemented, accessing both the same database record. Take these steps to provoke a concurrency exception:

  • Read the record for client 1 (press the Read Record button of the Client 1 group)
  • Read the record for client 2 (press the Read Record button of the Client 2 group)
  • Update the record for client 2 (press the Update Record button of the Client 2 group)
  • Try to update the record for client 1 (press the Update Record button of the Client 1 group)

The result should look like this:

Sample when Race Condition occurred

Some Sample Details

The sample code was created using Visual Studio 2013 Ultimate and Entity Framework 6.1. In case you use an older version of Visual Studio, you might have to create an empty solution and add the files to it.

The application expects a SQL Server instance (not SQL Server Express) installed on the local machine having the default server name (MSSQLSERVER). In case you do not have a SQL Server with this name running on your machine, you have to change the connection string in the app.config file before you can run the sample.

On the SQL Server, the application creates a database named EntityFrameworkRaceCondition. Please make sure to have the appropriate rights to create a database when you run the sample.

Links

Microsoft Data Developer Center Entity Framework

Entity Framework / Get Started / Code First to a New Database

Entity Framework Tutorial Update Entity Using DbContext

Wikipedia Race Condition

Sample Application

1 thought on “Handle Race Conditions / Concurrency in Code First Entity Framework Applications”

  1. multithreading concurrency terminology race – condition . Handling data races in general requires some programming discipline to induce happens-before edges between accesses Code which relies heavily on sleeps is prone to race conditions , so first check for calls to sleep in the affected code .

Comments are closed.