I am trying to synchronize threads in my SpringBoot application through pessimistic locks in the database. For the test environment, I use HSQLDB. But for some reason, when calling the SELECT ... FOR UPDATE
query, my all threads receive the result at the same time and continue to execute further in parallel. Although I expect threads will lock row sequentially. I use JPA and my code to get a pessimistic lock is very simple:
private EntityManager entityManager;
...
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void doSomething() {
sync();
...
}
public void sync() {
MyEntity entity = entityManager.find(MyEntity.class, entityId, LockModeType.PESSIMISTIC_WRITE);
log.debug("entity {} was locked", entity.getKey());
}
I use HSQLDB 2.6.1 and MVCC as concurrency control model. Does HSQLDB support pessimistic row locks?