NHibernate - How to apply optimistic lock while inserting the record?

Asked By abdul kudhus on 03-Feb-17 08:29 AM
I want to apply the optimistic-lock while inserting a record.when multi users are trying to insert a same record at the same timings then how can we notify the user with the help of optimistic-lock.I want to notify the user state that "record is already saved by some other user".
Robbe Morris replied to abdul kudhus on 07-Feb-17 06:17 PM
This type of situation really requires a more robust solution above and beyond locking a record.  Ideally, if two or more people are updating the same record columns, then you should communicate back to the user(s) who changed the record since they performed the query to view the record on their screen.  Typically one user will have more authority over other users and someone will need to make a decision as to what the appropriate column values are for the record.

You would typically have LastUpdatedByUserID and LastUpdateTime and also keep a class property for LastQueryDateTime (You'd set this in the UI of the app).  At update time, you'd check the LastUpdateTime and make sure it is <= LastQueryDateTime.  If not, you'd display the LastUpdatedByUserID and LastUpdateTime as part of the message informing the user that someone else had modified the record while they were working on it.  I've even build apps that show the phone number and email address of that user to make it easy for the two of them to work it out.  You may also want to have an override button to enable a user to force an update regardless of who modified it and when.