Link to home
Start Free TrialLog in
Avatar of jduawa
jduawa

asked on

Transaction locking

I am building an application in which multiple users could potentially edit the same record...I am wondering the best way to prevent this from happening.  Usera will click a link and bring up a form...that form already now what record in the DB it will need to update when the form is submitted.  While the form is updated i need to not allow others to update that same record.  When user a opens the form i can set a column in the DB that the record is open there by preventing others from modifying it, but what if the user hits the back button or closes the browser then that record should no longer be locked.  Hopefully this make sense
thanks
Avatar of mrichmon
mrichmon

It does make sense.

But that is a standard problem.  You can't avoid it.  The best you can do is a work around such as removing any locks that are older than 30 minutes or an hour or whatever time you want.  Then if the person does not have a lock when they save - you tell them a timeout happened.

That is the only way to clear locks for editing - by guessing.

Otherwise you can instead of locking do a complex test to see if it changed in the meantime and only allow save if it did not change.  But this is very complex to do.

if your records have an updateTime field, you can use that to see if the record has been updated since this user has retreived it. simply gab the update time when the user starts updating, send it to the page as a hidden field, then compare it to the current record before updating. if the time stamp from the users page doesnt match whats in the db, throw an error.
SOLUTION
Avatar of Plucka
Plucka
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jduawa

ASKER

All good methods, I will have to try and implement them...I suppose in most application where you compare the fileds that could have been updated to the originals and the second user actually needed to make a change then they would just have to repoen that record to get "new" original values and at that time they can make changes they would need to...Thanks