Postby bdoolan » Fri Jul 09, 2010 4:01 am
Hi Rich,
One way to softlock is to use the edition of the Jade objects. For example, consider the simple case where the webform doing the update displays the property values for just one Jade object. When the data is sent to the browser, the edition of that object can be sent in a hidden field along with the visible data. Make sure the hidden field is setup so that, when the user transmits the data back, the value of the hidden field is also sent along with the data. If the user updates the object, then before updating, compare the edition of the object now to the value when it was saved in the hidden fields. If the value of the edition has changed, you know the object has been changed by another user in the meantime and take appropriate action. Otherwise you can update with confidence that you won't have a lost update.
Remember when filling in the value of the fields on the form, ideally you should shareLock the object first and unlock at the end. So something like
sharedLock(obj); // lock object
hiddenEditionField := obj.edition; // deliberately AFTER the sharedLock, guaranteeing edition is up to date
webFormProperty1 := obj.property1; // assign webform property values
webFormProperty2 := obj.property2;
epilog
unlock(obj);
Later, on the update of the object, do
beginTransaction;
exclusiveLock(obj); // exclusive lock since it's going to be updated anyway and it will be exclusively locked shortly
if obj.edition <> webForm.savedEdition then
// object has been changed in the meantime
// handle error and return or raise exception to handle situation
// abortTransaction somewhere. This will unlock obj
endif;
// proceed with the update
obj.property1 := webFormProperty1;
obj.property2 := webFormProperty2;
commitTransaction; // this will release the exclusive lock above so no need to do this in the epilog
If the webform is more complex and you are updating multiple objects, all of which you want to guarantee haven't changed in the meantime, you can construct a string of the format <oid>-<edition>,<oid>-<edition> etc with one <oid>-<edition> pair for each object you want to protect. Send that string in the hidden data and then use the same technique as above after exclusive locking each object.
Cheers, Brendan