Search code examples
javaspring-bootspring-transactions

@Transactional rollback not working in SpringBoot


I have a @Component layer that calls two functions. I forced my second function to return an Exception but it the first transaction didn't rollback. This Service layer is annotated with @Transactional

@Transactional
class {

void updateMessageResult() {
    Message updatedMessage = messageService.updateResult(message, messageQueueDto);
    messagePublisher.publishEvent(updatedMessage);
 }
}

In publishEvent it forced to throw the RuntimeException

public void publishEvent()  {
throw new RuntimeException();
}

Update: I tried the suggestions but it still won't rollback


Solution

  • Sorry for posting the wrong code. I just typed the code instead of copy pasting it.

    The answer is I forgot to add the accessor (or public) for the method.

    If you won’t specify the accessor of method in Java, the default accessor would be default.

    Thank you!