I am using a PostgreSQL database with Spring Data JDBC in my Spring Boot application. When I wanted to introduce pessimistic locking to my findSeatById method in my SeatDAO, I thought if it is possible to use PESSIMISTIC_WRITE and PESSIMISTIC_READ interchangeably.
My idea is that if we use PESSIMISTIC_READ as the underlying lock type on the derived query method findBySeatId, modifying operations are blocked, i.e. we can read the seat of the passed id without any other method altering it. When setting the lock type to PESSIMISTIC_WRITE, we block read operations, so that no other method can read the referenced seat during this locking period, which implies that no other method can also modify it, since the seat can not be read and hence, not modified.
So can we use LockType.PESSIMISTIC_READ and LockType.PESSIMISTIC_WRITE interchangeably?