have you tried it?
What makes you think a lock won't propagate to the database (not saying it won't, but I'd be surprised if it didn't)?
You made me believe an actual advise was posted, anyway:
As reference says, the lock is honored at persistence-context level. Since two sessions are involved, there exists two different persistence-contexts, so EntityManager.lock() issued in one session has not enough 'reachability'.
Besides, the lock (either READ or WRITE) granted by EntityManager.lock() is not semantically equivalent to pessimistic lock. In other words no thread is getting blocked when accessing an already locked entity.
That is why I'm looking for expert advise.
I think, what you are looking for is the @Version attribute for your entity. This allows optimistic locking. See this for more details:
Let us know, if this doesn't serve your purpose.
No, it doesn't. I'm aware of potential disadvantages of pessimistic locking, however that is needed here.
So the question is how to implement pessimistic locking.