#350 UnexpectedRollbackException When Catching an Exception

Companies:
Unknown company
Themes:
SpringTransactionsCode Review

Explain how the code works:

@Component
class Bean1 {

    @Autowired
    private Bean2 bean2;

    @Autowired
    private EntityRepository entityRepository;

    @Transactional
    public void save() {
        Entity entity = new Entity();

        try {
            bean2.doIt(entity);
        } catch (Exception ex) {
            log.error("Error", ex);
        }

        entityRepository.save(entity);
    }
}

@Component
class Bean2 {

    @Transactional
    public void doIt(Entity entity) {
        throw new RuntimeException();
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in