Search code examples
springexceptioncachingtransactional

Spring unexpected behavior in service layer


I have a problem when calling a method to delete from the data repository.

In my service layers I have this two methods:

@Override
public User findById(long id) {
    List<User> users = userRepository.readUsers();
    return users.stream()
            .filter(u -> u.getId() == id)
            .findFirst()
            .orElseThrow(NoSuchElementException::new);
}

@Override
@CachePut(value = "users")
@Transactional
public void deleteUserById(long id) {
    userRepository.deleteUser(id);
}

The problem is that when deleteUserById is called, somehow the foundById is also called and raised an exception which I am catching in an @ControllerAdvice.

Deletion is successful when I check the database but when the call is executed the response is the error in the ControllerAdvice.


Solution

  • Found the problem, I was calling findById somewhere else after I deleted the user. Outrageous mistake! Sorry. Thank you for your time.