I'm working on Java and I have a "batch" procedure, with multiple objects, and each one of them does something on the db (mainly updates). I want some of them to wait each other, but only if a specific condition is encountered. To be precise, some of them may update the same entity, and Java gives an OptimisticLockException in that case, but some others may update other entities, so they don't have to wait. I would like to create a ReentrantLock (or something that works like that) with a specific ID (in this case, the entity ID), and only the ones which have the same ID may wait until it is unlocked, while the others can execute their code freely.
I don't know if the request is clear, as english is not my first language, sorry. But, I would appreciate if someone can help me!
Thanks in advance.