...
Panel |
---|
Example SituationTo set the scene, here's a typical situation in our web applications:
Obviously this isn't good; when Dave David finally submits changes, we need the app to detect that someone else has changed the data, and to handle the situation appropriately. Two aspects to doing this are:
We'll take these in reverse order: |
Panel |
---|
Handling a Concurrent Update SituationThere are three approaches to handling a concurrent update situation:
Unless there's a clear business requirement for the merge option, I think we need to take the second approach, "first commit wins". In order to pursue this approach, we do need to have a mechanism for detecting concurrent updates. Which leads to... |
Panel |
---|
DetectionHibernate does offer a facility for detecting concurrent updates by providing a version attribute. A database column (usually integer or timestamp) can be designated as a version field - for new tables we typically use the integer type. However, the way most of our web apps are written, this offers only limited protection. Generally, when a user updates data in a web app, there are two steps:
There can be a significant period of time between these two steps. In step 2 (posting of data to be updated), we typically do this in the same HTTP request:
Because we re-read the data, the version number check offered by Hibernate only will protect us from concurrent updates taking place during this request. As the request is typically very fast (<1s), this is unlikely to happen. What we want to do is to detect any changes in the database that occurred between the data-retrieval step (step 1) and the data-posting step (step 2) (i.e. while Dave David is at lunch). For this to happen, step 2 needs to know what the version field was in step 1. So far I've read about three techniques for doing this:
|
...