locking a page during transaction


I need to set a way to lock a page preventing someone else to make any changes in one page. Imagine someone is reviewing some numbers and editing them or whatever , and need to save them. However it can happen someone else (2 or more people ) are trying to access to such page and we need to lock them whenever someone else is already processing data - usually in a save button from a pop up in that page. How to do this locking mechanism? Thank you! 

First I would look to see if you can change your user flow so that locking isn't necessary. The problem with locking a page like this is what happens if the user goes to a page and walks away without hitting save, just closes the browser, looses internet connection etc? Result is you have an orphaned lock and nobody can access the page so you now need to implement a lock timeout etc. In most cases record locking on web based multi user applications tends to lead to headaches in the long run.

An alternative is to do a record compare when you go to save. When you first go into edit mode take a copy of the record then when you hit save compare that copy with the current database record and if they are different display the differences to the user and give them the option to either abort or to override. You could go more advanced and give them a merge option to pick which bits of the two records to keep.

If you really want to do the locking then you could include a locked user id and locked date attribute in your entity to record who has the record locked and check it on edit plus have a timer that cleans up locks older than a certain duration. If you have a lot of entities to lock then create a separate lock entity and have a link in each entity back to the lock entity.