Code through the pain Ladislav Mrnka's professional blog about software development


Data access code follows simple implementations patterns. One of very common patterns consist of fetching data from a database, modifying those data and persisting those data back to the database in the same transaction. Using this pattern in any concurrent environment where multiple threads (or processes) can work with the same record concurrently results in new challenges. How should our application handle scenario when another thread updated the record after the first thread fetched the record from database but before it was able to persist its changes back to the database? This area of database access implementation is in general referred as concurrency. We differ between two common solutions: Optimistic concurrency handling and Pessimistic concurrency handling.

Optimistic concurrency uses a special column in the table to track if the record was changed. The main requirement is that the column must change its value every time when the record is updated. Each relational database offers a special data type for this purpose which is automatically updated every time its record is updated. Microsoft SQL Server offers deprecated TIMESTAMP and newer recommended ROWVERSION data types for this purpose.

If we use Optimistic concurrency to solve the initial scenario with two threads the first thread will load the record together with an actual value of the time stamp and when it tries persisting the change it uses time stamp's value in WHERE condition of UPDATE command together with the record identification (for example key). If another thread updates the record in the mean time the update from the first thread doesn't find any record matching its WHERE condition. This will result in 0 updated records (in Microsoft SQL Server this information is received by executing SELECT @@ROWCOUNT query after the update command) and the thread should somehow react to this situation. The reaction follows common pattern of reloading the record to get current values, applying changes to reloaded data and persisting them again. The real implementation of this pattern may involve for example user interaction where a user must resolve the conflict. Entity framework has direct support for Optimistic concurrency in both ObjectContext API (also shows required mapping in EDMX) and in DbContext API. Code first supports mapping in both data annotations and fluent API.

Pessimistic concurrency doesn't use any special column but it instead uses transactional and locking mechanisms offered by relational database. The whole idea of pessimistic concurrency can be compared to critical section. The fetching of the record, its update and followed persistence must be considered as atomic operation which can be executed by only single thread (per record or record set). When the first thread enters this critical section no other thread can modify the record or enter critical section for the same record. Using this mechanism requires understanding of transaction isolation levels and locking mechanism in used database. The bad news is that Entity framework doesn't have any built-in support for Pessimistic concurrency.

This article was inspired by question asked by Ari on Stack Overflow. The rest of the article will show why base Entity framework update patterns do not provide pessimistic concurrency and why approach described in the mentioned question from Stack Overflow doesn't work. The article will also provide a real solution for pessimistic concurrency which requires usage of native SQL.

Posted on September 29, 2012 by Ladislav Mrnka
Filed under: Entity framework
Continue reading

I have already wrote the article about Microsoft strategy to annoy as many of their current Windows Phone customers as possible by announcing that their phones will not receive upgrade to Windows Phone 8 and that they will only receive Windows Phone 7.8 with a new fancy home screen - that is still the only officially announced feature for the upgrade. But somebody in Microsoft believes that annoying customers is not enough so they decided to annoy developers as well. The rest of this article is about features in Windows Phone 7.8 and about situation around Windows Phone SDK 8.0.

Posted on September 23, 2012 by Ladislav Mrnka
Filed under: Microsoft
Continue reading

This article is about "improvement" in Visual Studio 2012 unit testing. I think it doesn't happen very often to have an upgrade which makes the functionality worse than the previous version. In the case of the new Test Explorer window we can even talk about removing previously available functionality because the new window is mostly useless for any bigger solution. Until Microsoft improves the window we are not away from the testing experience known before Visual Studio 2008 when we didn't have any built in support for unit testing and we had to use third party plugins or external test runners.

I wanted to write this article immediately after I tried the new Test Explorer in Visual Studio 2012 Ultimate RC but I still believed that Microsoft will improve it before launching Visual Studio 2012 especially when complains to the new Test Explorer appeared long time before RC. Unfortunately Visual Studio 2012 RTW was released with the same Test Explorer window and there is also no update available yet. Test Explorer window should be hopefully improved this fall. There is also related request on Visual Studio User Voice with few other suggestions for improvements in comments.

The rest of the article describes what is wrong with the new window and compares the functionality with Visual Studio 2010 and Resharper 7.

Posted on September 16, 2012 by Ladislav Mrnka
Filed under: Visual Studio
Continue reading

Apple's iPhone 5 keynote ended more than hour ago. It didn't bring any big surprise and even didn't convince me to change my iPhone 4 for the new iPhone 5 because I don't need a new phone and a larger screen or LTE which is not available in my country do not change it. I'm using tablet when I need mobile device with a larger screen and more computing power. The disappointment is missing NFC support because in my opinion, it means either that Apple doesn't believe in that technology yet or that the technology didn't succeed on the main markets.

Anyway I didn't start this short post to discuss the new iPhone. I wanted to watch the live stream of the keynote. One could think that it should be enough to turn on Apple TV and simply select a keynote and stream it to my TV or run iTunes on iPad/MacBook/PC select keynote and stream it but no luck. I didn't find the live stream and I believe the reason was quite simple - Apple didn't provide any. I know, I will be able to watch it as a podcast but that is not the same as streaming it live. There are rumors that Apple wants to bring streamed TV content to Apple TV. How Apple wants to do that when they are not able to stream their own keynote? This was a huge disappointment - bigger than anything in the keynote itself.

Btw. the new iPod Nano looks more like Lumia Nano ...

Posted on September 12, 2012 by Ladislav Mrnka
Filed under: Apple