nyambali36
(Mathew Nyambali)
November 10, 2024, 7:15am
1
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,
krivopustov
(Konstantin Krivopustov)
November 27, 2024, 7:42am
3
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
nyambali36
(Mathew Nyambali)
December 5, 2024, 11:05am
5
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()); ```

krivopustov
(Konstantin Krivopustov)
December 9, 2024, 6:48am
7
Can you provide a test project demonstrating the problem?