I'm trying out the @Transactional
annotation but I'm not sure what's exactly wrong, I have the following code
Controller
@RequestMapping(value = "/remove", method = RequestMethod.POST) void removeUserFlight(@RequestParam("flightId") Long flightId) throws Exception {
int affectedRows = userFlightDao.removeFlightByFlightId(flightId, user.getId());
throw new RuntimeException("Testing rollback?");
}
Repository
@Repository
public interface UserFlightDao extends CrudRepository<UserFlight, Long> {
/** Named DELETE query defined in {@link UserFlight} */
@Modifying @Transactional int removeFlightByFlightId(Long flightId, Long userId);
}
I was on the impression that the record will not get deleted if an exception gets thrown after the executing line. Could anybody please clarify what's wrong here?
edit: I also tried to put @Transactional on the Controller method, but it made no difference.
edit2: Tried to move the logic to a service like suggested below by Ralph, it worked fine. I still have no clue why @Transactional on the controller method directly doesn't work. I'll consider this solved for now but I'm still confused.