xkingpin
xkingpin

Reputation: 641

Lock page / entity edits if another user is editing

I have an asp.net page to edit an entity. What is the best design pattern to notify and prevent saving when another user is already on the edit page for that entity. I would want the lock to expire when the user leaves the page. I would also like to display the user name that has the page locked.

Session state could be either inproc or state server.

The user needs to know before attempting to save (for example disable the save button for all other users on the edit page)

Upvotes: 0

Views: 629

Answers (1)

Ross Overstreet
Ross Overstreet

Reputation: 370

There are many different ways to do this, but in general it is fairly complex. One challenge to be aware of is that you don't know when a user leaves a page as there is no way to signal that.

One approach that I've seen used a few times is to do the following:

  • Add a column to identify who has the record locked (e.g. add Locked_User_ID etc to your table)
  • Add a column to identify what date/time the record was locked. (e.g. Locked_Timestamp)
  • Define a global constant (config setting, etc) specifying how long a lock is good for.
  • On page/controller load, check to see if the record in question is locked, and if so, was it locked recent enough (based on your global constant) to believe that another user is still working on it. If so, don't allow editing and display the user that is editing and their last activity (based on Locked_Timestamp & Locked_User_ID).
  • If it was not locked recently enough, or is not locked at all, or is locked by you, update the Locked_User_ID to the current user and Locked_Timestamp to the current date.
  • Once the user clicks back/cancel, save, etc, unlock the record. (that way it doesn't have to wait for the timeout based on the global constant - but in the event they just close the browser, etc the timeout will be the failsafe).

Another slight variation of this to have a "check-out" or "edit" button/functionality that implements this - so that users can view information without automatically acquiring a lock on it, and only once they click "check-out" or "edit" does it check and acquire the lock.

Hope this helps!

Regards,

Ross

Upvotes: 5

Related Questions