Monday, October 17, 2011

[Tutorial] Code First with Entity Framework 4.1
Part5: Concurrency Management & Conclusion

Assuring the consistence and coherence of data in the database is a very important topic. Concurrency problems come up when multiple source (clients, systems, etc…) try to modify data at the same time.Your application must be able to choose whether to accept or reject any data changes that arrive simultaneously.

Simultaneous execution of transactions must provide the same results as sequential execution of those same transactions.If you achieve this goal then your system is well prepared for correct concurrency handling.

Entity Framework 4.1 includes a very complete concurrency approach. The DbContext class that we already saw before allows for efficient concurrency handling.

First Wins

This resolution strategy defines that the first modification wins if there are concurrency conflicts. All following modifications on the same data are ignored and clients get exceptions that tell them that their transactions did not complete because the data was already changed.

You have to determine which properties might be subject to concurrency problems. You may have properties which do not need any concurrency handling. A property that contains the last modified date might not need to implement any concurrency handling for example.

All properties that must be checked for concurrency need to have the [ConcurrencyCheck] attribute set as shown below:

image

If you don't like using attributes, you may also use the CodeFirst Fluent API on your properties to activate concurrency checking:


image

You have to add concurrency checks only on properties that really need it since this will have some impact on performance within your code. The more properties with activated concurrency checks you have the worse your performance might be depending on your implementation !

image

In the example above we retrieve a manager object from the database, store it in memory and change some property values (but only in memory). We then change some values on the same data directly in the database to simulate concurrency as if two clients did modifications at the same time.

When we call the SaveChanges() method you will see that we get an exception of type DbUpdateConcurrencyException. This is where you need to add your custom concurrency conflict resolution logic. In this example we decided to guard the first modification (First Wins). In the last step we update the context in memory with the data that was changed in the database via ex.GetEntry(context).Reload().

Last Wins

This is the default resolution strategy of EF 4.1 in case of concurrency conflicts. It defines that the last modification wins if there are concurrency conflicts. All previous modifications on the same data are overwritten. Clients only get informed if they use the [ConcurrencyCheck] attribute, some kind of log, some event or other treatment.

image

The example above shows how to implement a Last Wins approach. If there are exceptions due to concurrency conflicts we store the new initial value in the context thus synchronizing with the source. We then retry saving the data until it is saved to the database and the processing is successfully completed.

Conclusion

I hope this tutorial gave you a first insight on the new features of EF 4.1 which are quite powerful even for code purists. With the new features presented in this tutorial EF becomes one of the most complete products on the ORM market. Today, it is the only product that proposes all 3 approaches
CodeFirst, Database First and Model First at the same time.

Some features are missing like auto-synchronization of the database if the class structure changes and stored procedure integration but they will be added in the next weeks / months.

Share/Save/Bookmark

2 comments:

Spider said...

Hi Jason,
This is a marvelous work. I feel lucky by visiting your blog. You really gave me a huge acceleration in my work. Please keep publishing more. Thanks a lot.

Jason De Oliveira said...

Thank you very much for this encouragement. I am happy that you like my blog.

I will continue publishing tutorials and infos concerning .NET and the latest technology trends. So stay tuned!