Optimistic locking lockmanager usage

Hi,

I’ve installed the optimistic locking addon and trying to use it for this use case, in a ticketing system i want to lock an entity when booking a seat and unlock right after. Just want to confirm if this is the proper usage, theres not much documentation and examples


@Autowired
    private LockManager lockManager;

//

FetchPlan segFetchPlan = fetchPlanRepository.getFetchPlan(Segment.class, "segment-tickets");
        Segment segment = dataManager.load(Segment.class)
                .id(segmentId)
                .fetchPlan(segFetchPlan)
                .one();

        try {
            lockManager.lock(segment);
			
			//execute some logic
            
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalStateException("Exception message", e);
        } finally {
            lockManager.unlock(segment);
        }

regards,

Hi Mathew,

We’ve added an example of custom code using the pessimistic locking to the docs:

@Autowired
private LockManager lockManager;

public void lockAndProcessDocument(Document document) {
    LockInfo lockInfo = lockManager.lock(document);
    if (lockInfo != null) {
        throw new IllegalStateException("Document is already locked by " +
                lockInfo.getUsername());
    }
    try {
        processDocument(document);
    } finally {
        lockManager.unlock(document);
    }
}

Hope this helps.

Regards,
Konstantin

1 Like

Whenever I try to lock an Entity, the method returns LockInfo, which indicate that it’s already locked by someone but the username is null. Im running this code in an editor screen but it a different entity’s editor screen

        
        System.out.println(" ");
        System.out.println(" lockInfo: "+ lockInfo);
        System.out.println(" ");
        System.out.println("Lock details  ");
        System.out.println(" lockInfo objectId: " + lockInfo);
        System.out.println(" lockInfo id: " + lockInfo.getId());
        System.out.println(" lockInfo date: " + lockInfo.getSince()); ```

![image|683x186](upload://ck46hLkvBUxYjhWpl14fTjyD5UM.png)

image

Can you provide a test project demonstrating the problem?