Copy link to clipboard
Copied
I'm designing quite a large web app, which will be used by numerous peole at the same time.
Is there a way I can lock the page if someone is on it?
What the best way to go about it?
Thanks
Copy link to clipboard
Copied
Let's see... what do you mean by "lock the page?" That's generally not the approach that is taken in web-apps. Let me explain . . .
Any time that you allow more than one user to update records in a database at the same time, you must ensure that conflicting changes do not occur, such that one user's changes do not overwrite another's. There are two ways to do that: optimistic and pessimistic:
Optimistic locking strategies are most often used in web applications because of the asynchronous nature of the HTTP protocol, as well as the need to support potentially thousands of users.
Probably the most common approach that is used for change-detection is a "change counter" field. It's a simple integer, and when you retrieve the record you remember what it is. Then, later, your SQL UPDATE statement looks something like this (assuming you "know" that the change-counter had the value 123456😞
UPDATE blahblah ...
SET ... blahblah ... , change_counter = change_counter + 1
WHERE ... blahblah AND ... change_counter = 123456
The statement will fail if the value of the change-counter isn't what you expected it to be, having updated none of the other fields.
Some applications also use a "lock field," which is a boolean field that can be True or False. Variations include a "lock timestamp," designed to automatically provide for lock-timeout when records are abandoned (a distinct possibility). Applications check this field upon retrieving the record and turn-away users from records that are now locked. But some mechanism must be provided to handle "stale locks," and the overhead of these mechanisms can be considerable.
Typically, it is necessary to use both a "lock field" and a "change counter" to prevent conflicts.
Do other mechanisms exist in the context of ColdFusion? Certainly yes. But memory considerations remain, as does the need to be able to periodically restart the web-server processes without thereby losing change-control information. Time and overhead are also an important issue: "optimism" is efficient while "pessimism" is costly.
Copy link to clipboard
Copied
I prefer the indifferent approach. If Person A can overwrite Person B's data 5 minutes later, why invest time and effort into preventing him from doing it 5 nanoseconds later. If the data is the same, it doesn't matter at all. If the data is different, how do you know who was right?
Copy link to clipboard
Copied
I hope that was intended as humor.
The issue here, obviously, is when two people (unbeknownst to one another) are entering a record at the same time.
Both edits appear to "succeed," but one of them overwrites the other. Nobody knows.
Obviously this is an unacceptable situation, and this is precisely what the change-counter logic will successfully address. Both users upon beginning their edit-sessions capture the same change-counter value. When the first user makes his update, it succeeds and the counter is incremented. When the second user tries, it fails because the change-counter is not the same as it was.
"Indifference" has nothing to do with it: it's a serious bug that might not be detected in testing (if you actually do any) or if you only do this for a hobby in "casual" use.