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 ?
Asked
Active
Viewed 1,360 times
0

idrak_07
- 19
- 5
-
What do you mean by "transaction could not be happened"? – Simon Martinelli Jun 13 '22 at 08:55
-
I tried to make a http call to another service, but it returned timout. So it goes to Catch section without making any transaction (Like update). Then I need to release that lock immeadiately – idrak_07 Jun 13 '22 at 09:49
-
2I would say you should not lock anything if you have no tx in the first place. – Antoniossss Jun 13 '22 at 10:30
-
You rollback the transaction; this will unlock all locks it holds in the database. – Chris Jun 13 '22 at 15:21
1 Answers
1
You can explicitly unlock by
entityManager.lock(entityObject, LockModeType.NONE);

Ali Naser Khan
- 26
- 3
-
That would be implementation specific as the JPA spec says nothing about unlocking locks - there are other lock types, and all this does in the spec is get (or check) a lock in the database. Optimistic locking for instances verifies and increments the version - only a rollback 'unlocks' or undoes the version change. – Chris Jun 13 '22 at 15:25
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 15 '22 at 01:13