Database wins

The database wins approach would allow the system/application to discard the client/UI changes and override the update operation using the latest data loaded from the database.

The following code shows how the concurrency conflicts are handled in the database wins approach. It reloads the entries from the database using Reload(), discards the client data in favor of the database, and performs the SaveChanges() operation to persist the updated changes from the database. The following code performs handles database first concurrency in the Post persistence:

     public async Task<IActionResult> Edit(int id, [Bind("Id,Title,Content,
Summary," + "PublishedDateTime,Url,VisitorCount,CreatedAt,
ModifiedAt, BlogId,AuthorId," + "CategoryId,TagIds,FileId,
Timestamp")] Post post, IFormFile headerImage)
{
if (id != post.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
var transactions = new TransactionScope();
try
{
// Code removed for brevity
}
catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
{
if (!PostExists(post.Id))
{
return NotFound();
}
else
{
try
{
dbUpdateConcurrencyException.Entries.Single().Reload();
await _context.SaveChangesAsync();
return RedirectToAction("Index");

}
catch (Exception exception)
{
ExceptionDispatchInfo.Capture
(exception.InnerException).Throw();

}
}
}
}
// Code removed for brevity
}

We have seen how the database wins approach handles the concurrency conflict. In the next section, let's explore how the client wins approach handles the concurrency conflict.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset