Locking-Hints

MichaelaReydt
Community Manager
Community Manager
1 0 1,170

There are two general hints you should keep in mind, when you are locking objects:

1.) put locking-orders in a try- or finally-block

The try-/finally-block makes sure, that the locked object will be released again in any case - even if an error appears.

Otherwise the object will be locked on the server for the whole runtime of the session, which locked the object. This could be equivalent to the runtime of the server if a server-script locked the object.

2.) never ever lock your objects recursive unless you need to

In most cases a non-recursive lock is want you want, so you should get into the habit of only using recursive lockings when you need them. Recursive locks may make sense if you:

  • want to lock a Page including all Sections
  • want to lock a folder and all childrens to perform a bulk operation

Example code

boolean wasLocked = medium.isLocked();
try {
     if ( ! wasLocked) {
          medium.setLock(true, false); // lock non-recursive
     }
     medium.release(false);     // non recursive release
} catch(LockException lockFailed) {
     context.logError("lock failed -> medium already locked by " + lockFailed.getUserLoginName());
} finally {
     try {
          if ( ! wasLocked) {
               medium.setLock(false, false);     // unlock non-recursive
          }
     } catch(Exception unlockFailed){
          // proper error handling
     }
}

Version history
Last update:
‎04-20-2011 06:39 AM
Updated by: