I am using Postgres in my spring boot application , I am selecting a record from table and the same record can also be modified by another process.
I was using PESSIMISTIC lock as below,
public interface EntityRepository extends JpaRepository<Entity, String> {
@Lock(LockModeType.PESSIMISTIC_WRITE)
findByIDwithLock()
}
But due it's locking performance impact I am trying to move to OPTIMISTIC lock. I have introduced a Long field with @Version in Entity class and it works fine in the below scenario,
If multiple threads in the same process updates the same record - the versions are incremented and values getting updated as expected
From OPTIMISTIC lock's articles, it does not put a lock on the record. But when I try to update the same record from another spring boot application or even manually running a update query the update is waiting for the other process to commit the updates. If it is not locking why the update from other process to same record has to wait?