cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Locking a SailPoint object

Locking a SailPoint object

Here is a code snippet to properly lock and unlock an object with using the transaction locking mechanism. The example is for a custom object, but can be used for any sailpoint.object that extends SailPointObject. For an Identity object and other objects (Certification) that should use the persistent locking mechanism, refer to the lockIdentity, lockCertificationById methods in the ObjectUtil class.

 

import sailpoint.api.ObjectUtil;

import sailpoint.object.Custom;

 

//Get the Transaction lock

 

Custom custom = ObjectUtil.transactionLock(context, Custom.class, custObjName);

 

try {

        //Modify the object

        custom.put(key, val);

        //Save the modifications in the Try block

        context.saveObject(custom);

} catch (Exception e) {

        //Log any errors encountered during saving the object

        log.error(e);

} finally {

        //Commit the transaction

        context.commitTransaction();

}

context.decache(custom);

Labels (1)
Comments

Can be done using IIQ Console too. What is the advantage of using this code?

If you are writing a rule or script within your implementation, you won't be able to lock objects from the console.  If you need to lock an object from within your rule/script, this is how you do it. The "...update the item..." line is a placeholder for other code in your rule/script which acts upon the object.

This makes sense. Thank you very much.

That code looks hot.

By the way, how does the system maintain transactional locks?  Is it just kept in memory until the transaction is complete?  What happens if something attempts to modify an object that has been transactionally locked?

Hot as in "stolen"! Yes, thanks again.

The transaction lock is maintained by a database row lock. If another process tries to achieve a lock, the transaction should be blocked at the database layer. If you are not seeing that behavior, there could be a database configuration issue. For SQL Server - the isolation level should be read committed snapshot. This is now an out of box configuration for SQL Server. I believe the other supported databases have the proper isolation configured out of box.

Hi Mike,

Please tell how to set DEFAULT_LOCK_TIMEOUT for transaction locks. In the document I saw that the definition for this is - Default number of seconds waited before attempting to obtain a persistent lock before throwing ObjectAlreadyLockedException. That means this parameter is only valid for persistent locks. I tried this by using this API -

ObjectUtil.lockObject(context, Custom.class, null, "Custom object File name", PersistenceManager.LOCK_TYPE_TRANSACTION, 

                                                                     Util.uuid(), ObjectUtil.DEFAULT_LOCK_TIMEOUT);

But it is not working. Please tell how to set the default lock timeout for transaction locks. Also what is the default time a process wait for obtaining transaction lock if the object is already locked and it is not released yet. If there is no feature to set the default lock time out then how can we achieve this?

Thanks,

Mahesh

I tried to get the transaction lock on one of my custom object.

Both threads can get the lock at the same time, not sure if it's the intended behavior...

ObjectUtil.lockObject is a different story, it seems to work fine.

 

Dear community,

thanks for this very helpful Wiki post.

I'm wondering how this could be used in cases when multiple threads are potentially trying to update the same object?
It seems that ObjectUtil.transactionLock() is NOT visible to other threads.

Could you please kindly share any best practices how to lock a "Custom" object (or also a bundle) in a way that other
threads can retrieve that information.

Thanks in advance.

Best regards,
Daniel

Version history
Revision #:
2 of 2
Last update:
‎Jul 13, 2023 05:33 PM
Updated by: