Alex Rivera | Logout

Best way to handle concurrency issues

Asked 2009-11-22T17:17:51.387
12

i have a LAPP (linux, apache, postgresql and php) environment, but the question is pretty the same both on Postgres or Mysql.

I have an cms app i developed, that handle clients, documents (estimates, invoices, etc..) and other data, structured in 1 postgres DB with many schemas (one for each our customer using the app); let's assume around 200 schemas, each of them used concurrently by 15 people (avg).

EDIT: I do have an timestamp field named last_update on every table, and a trigger that update the timestamp every time the row is update.

The situation is:

  1. People Foo and Bar are editing the document 0001, using a form with every document details.
  2. Foo change the shipment details, for example.
  3. Bar change the phone numbers, and some items in the document.
  4. Foo press the 'Save' button, the app update the db.
  5. Bar press the 'Save' button after bar, resending the form with the old shipment details.
  6. In the database, the Foo changes have been lost.

The situation i want to have:

  1. People Foo, Bar, John, Mary, Paoul are editing the document 0001, using a form with every document details.
  2. Foo change the shipment details, for example.
  3. Bar and the others change something else.
  4. Foo press the 'Save' button, the app update the db.
  5. Bar and the others get an alert 'Warning! this document has been changet by someone else. Click here to load the actuals data'.

I've wondered to use ajax to do this; simply using an hidden field with the id of the document and the last-updated timestamp, every 5 seconds check if the last-updated time is the same and do nothing, else, show the alert dialog box.

So, the page check-last-update.php should look something like:

<?php
//[connect to db, postgres or mysql]
$documentId = isset($_POST['document-id']) ? $_POST['document-id'] : 0;
$lastUpdateTime = 
Edit
Report

1 Answer

0

Polling is rarely a nice solution.
You could do the timstamp check only when the user (with the open document) is doing something active with the document like scrolling, moving the mouse over it or starts to edit. Then the user gets an alert if the document has been changed.

.....
I know it was not what you asked for but ... why not a edit-singleton?
The singleton could be a userID column in the document-table.
If a user wants to edit the document, the document is locked for edit by other users.

Or have edit-singletons on the individual fields/groups of information.

Only one user can edit the document at a time. If another user has the document open and want to edit a single timestamp check reveal that the document has been altered and is reloaded.

With a singleton there is no polling and only one timestamp check when the user "touches" and/or wants to edit the document.

But perhaps a singleton mechanism doesn't fit your system.

Regards
   Sigersted

answered 2009-11-23T22:05:01.923

Your Answer