Creating Custom Locks

Hello

I wanna create an application whereby i can lock records or tables or both when one user is reading or making changes to the record and/or table. i wanna create custom locks each with their very own unique constraints/conditions and i wanna create it in the service… how do i go about doing that? how do create locks in Cuba and how do i add my custom constraints on them.

Regards

Hi Susan,

You can use the pessimistic locking described in the documentation. Below is an example of using this mechanism in a middleware service:

@Inject
private LockManagerAPI lockManager;

public void someProcess() {
    LockInfo lockInfo = lockManager.lock("someProcess", "");
    if (lockInfo != null) {
        log.info("Process is running by " + lockInfo.getUser() + " since " + lockInfo.getSince());
        return;
    }
    try {
        // do something...
    } finally {
        lockManager.unlock("someProcess", "");
    }
}

Hi Konstantin,

there is an error in this line:

log.info("Process is running by " + lockInfo.getUser() + " since " + lockInfo.getSince());

it say: “Log cannot be resolved to a type”

how do i fix that?

Thanks in advance.
Regards,

Add log declaration to your class. See an example here: Logging - CUBA Platform. Developer’s Manual

this line:

LockInfo lockInfo = lockManager.lock(“someProcess”, “”);

what am i supposed to put between the Parentheses?

Anything. In the first place, the API is used for locking entities, and the parameters are used for entity name and ID (see Javadocs). In your case, the combination of parameters should uniquely denote your process or service.