Published: 2011-11-26
Tagged: lost update problem, relational databases, ruby on rails, transactions, web applications

The last part of the series features a hands on demonstration. We will create a web application from scratch and apply the rather theoretic concepts discussed in the previous posts.

The Lost Update Problem - Part 3 : Applied Prevention

I discussed the problem of lost updates in the two previous posts:

  1. The lost Update Problem in the Context of Transactions, and
  2. Preventing Lost Updates in Web Applications.

This post demonstrates how to prevent lost updates within a Ruby on Rails web application. The focus of this post is practical, in the sense of getting the job done with a minimal understanding of the concepts. The two previous posts contain all the background information. The main part of this post is a screencast. It shows the complete process of hacking out the demonstration, and it features also two distinct ways to prevent lost updates. The links to the code, as given in the last section, provide a concise access to the relevant content.

The Screencast

  1. 0:00 Set-Up
  2. 4:00 Lost Update - Demonstration
  3. 7:00 Prevention from the Application Layer
  4. 23:30 Prevention from the DB Layer


All of the code is hosted on Github in the lost-update-demo project.

The demonstration of the problem itself is featured in the lostupdate branch. The code as generated by the accounts scaffold is unmodified.

The applayer branch prevents lost updates purely from within the application layer. The accounts controller contains all the adjustments in the create, and in particular in the update action.

Finally, the storedproc branch, tackles the problem from within the database using triggers and stored procedures. The code for those is contained in the corresponding migration. There is some support from the application layer which is located in the update action of the accounts controller.