I have a Spring @Service
bean in which some data saving in a MySQL occurs. It's saving LocalDateTime
objects, for booking rooms in future dates.
The problem is: when booking multiple rooms at once and one of them is not available (i.e. already booked), Spring should rollback all the rooms that were booked already, but isn't doing it. Everything but the error one is commited to the database.
Code inside my @Service
, the one that should rollback after an exception is thrown.
@Transactional(rollbackFor = { RecurrentMeetingBookingException.class})
public void bookRecurringMeeting(RecurrentMeeting meeting) throws RecurrentMeetingBookingException {
LocalDateTime start = meeting.getMeetingEntity().getStartDate();
LocalDateTime end = meeting.getMeetingEntity().getEndDate();
while (start.isBefore(meeting.getRecurrenceEndTime())) {
MeetingEntity recurrent = copyOfRecurrent(meeting, start, end);
try {
bookRoomForDate(recurrent);
} catch (MeetingException e) {
throw new RecurrentMeetingBookingException(e.getMessage());
}
start = start.plus(1, meeting.getRecurrenceType().getUnitOfTime());
end = end.plus(1, meeting.getRecurrenceType().getUnitOfTime());
}
}
I tried throwing a RuntimeException
too, but it also didn't work.
When returning a ResponseEntity
from my Controller
, the exception is indeed thrown, as I return Room is not available
as an exception, returning it's message.
What is the problem with my @Transactional
? Shouldn't Spring/Hibernate rollback everything when an exception is thrown? What can I do to solve this issue, and correctly rollback the @Transactional
?