public void findAndDelete(Long id) {
User user = userRepository.findById(id);
Long rowsAffected = userRepository.deleteById(id);
}
I’m using Spring data and not using any explicit @Transactional
annotation here. Is Spring Data by any chance, smart enough to make sure that both these are executed in a single transaction, so that I don’t have surprises like the user exists when fetched, but the user may get delete by another thread before I delete it in the next statement?
I assume if I don’t have the @Transaction
Spring will use a separate transaction for each and every database call I do within the method, despite them being kinda related operations. Is that correct? Or will Spring automatically identify and use a single transaction for both of the above operations?