I am using spanner database with Spring boot and JPA. I have a requirement like, whenever employee information is read from employee table, no-one can read/update/delete the the same row information until transaction gets completed.
I got to know for such purpose "PESSIMISTIC_WRITE" lock is used in JPA. (referred this link JPA PESSIMISTIC_WRITE on applications with multiple instances)
I tried to use it by using below code but its not holding the lock. When I try to hit the same query from other instance its giving result. Ideally It should wait until first transaction gets completed but still its giving the result.
Could you please suggest me any solution for it.
I am using below code to achieve the same.
@Repository
public interface EmployeeRepository extends JpaRepository<EmployeeEntity, Integer> {
@lock(LockModeType.PESSIMISTIC_WRITE)
@query("SELECT c FROM Employee c WHERE c.empId = :empId)
public EmployeeEnity getEmployee(@param("empId") Integer empId);
}
@Service
public class EmpService implement IEmpService {
@Autowired
EmployeeRepository empRepository;
@Transactional
@Override
public EmployeeEntity getEmployee(Integer empId) {
return empRepository.getClientId(clientId);
}
}