Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a web application that is accessed by multiple users from different locations worldwide. Let's say I have an "edit" form for a specific document and two (or more) users are editing the document at the same time. I know that both of the users can edit the document and submit it, but user1 will not see that user2 made changes to the document and might submit an older version of it.

I was thinking about adding some kind of a lock, but then, if user1 only opened the document for edit, but never changed anything (and kept it open) user2 will never be able to edit it. Therefore, I was thinking to add a Timeout for the editor, but then user1 might time out before he finished doing his changes to the document (let's say, went out for launch).

The question is, how would one prevent the document from being edited from one user while the other changes it?

Note: if this question does not belong here, please explain why and where could I ask such a question.

share|improve this question
    
What you're basically asking is, "How do I implement pessimistic concurrency in my application?" –  Tombatron Aug 11 '13 at 22:36
    
I might, but i don't know. will google that. –  Dementic Aug 11 '13 at 22:39
    
If you want to go for the locking approach, one possibility is to take out the lock by sending an AJAX request when the user actually starts editing the fields (rather than when they load the page). –  Cyanfish Aug 12 '13 at 0:47

1 Answer 1

up vote 3 down vote accepted

The basics of implementing optimistic concurrency aren't that hard.

First, you'll need a way to identify a row (or series of rows) in your database (think version number).

Second, when your application loads the data for your user's to review or edit, load the version of the data as well (see above).

Third, when your user goes to save their changes, you'll want to ensure that the version of the data they're changing matches the version of data that exists in the database. If the version numbers don't match then you'll raise and handle some kind of stale data exception.

share|improve this answer
    
if the form is a complicated one, which takes time to fill in, and then i raise the second user an exception, he will need to either refresh and re enter the data, or, i could show him what changed, but again, that might be a big list... any idea how can i handle such a case? –  Dementic Aug 11 '13 at 22:47
    
You could perform a quick AJAX request to check on the status of the data before you actually submit the form. That way you could ensure that the version of the data is acceptable before the user's form is submitted. –  Tombatron Aug 11 '13 at 22:50
    
Also, if the data the user is working on happens to be stale, an AJAX request could return the non stale values... –  Tombatron Aug 11 '13 at 22:51
1  
@Tombatron what happen if data changed after ajax request and before form submit? Also, concurrency without locking is optimistic concurrency, not pessimistic. –  imel96 Aug 12 '13 at 2:10
    
@imel96 egg on face. You're right. –  Tombatron Aug 12 '13 at 12:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.