springspring-boothibernatejpapessimistic-locking

How to unlock @Lock(LockModeType.PESSIMISTIC_WRITE) if there is no transaction


I have put @Lock(LockModeType.PESSIMISTIC_WRITE) on a query and working on it in service layer with @Transactionsal annotation. The lock is acquired successfully. But for some cases, the transaction could not be happened. And this lock takes time to be released. My concern is how can I release the lock immediately ?


Solution

  • You can explicitly unlock by

    entityManager.lock(entityObject, LockModeType.NONE);