Though ADO.NET and other ORM framworks like EntityFramework and Dapper support async pattern, you should remember that database drivers (at least all we know about) do not support concurrent db commands running against a single connection.
To see what we mean consider a bug we have recently identified. Consider a code:
await Task.WhenAll( newImages. Select( async image => { // Load data from url. image.Content = await HttpUtils.ReadData(image.Url); // Insert image into the database. image.ImageID = await context.InsertImage(image); }));
The code runs multiple tasks to read images, and to write them into a database.
Framework decides to run all these tasks in parallel. HttpUtils.ReadData() has no problem with parallel execution, while context.InsertImage() does not run well in parallel, and is a subject of race conditions.
HttpUtils.ReadData()
context.InsertImage()
To workaround the problem we had to use async variant of a critical section. So the fixed code looks like this:
using(var semaphore = new SemaphoreSlim(1)) { await Task.WhenAll( newImages. Select( async image => { // Load data from url. image.Content = await HttpUtils.ReadData(image.Url); await semaphore.WaitAsync(); try { // Insert image into the database. image.ImageID = await context.InsertImage(image); } finally { semaphore.Release(); } })); }
So, in the async world we still should care about race conditions.
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u