1 year ago

#364122

test-img

makozaki

Does JpaTransactionManager support REQUIRES_NEW propagation?

In my springboot app I have a method with nested transaction as follows.

//message handler
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Override
    public void processMessage(MessageDto messageDto) {
            // some code here that throws exception and marks transaction as rollback-only
            someService.setMessageStatus(messageDto, SUCCESS);
        } catch (Exception e) {
            messageDto.setException(e);
            someService.setMessageStatus(messageDto, ERROR);
        }
    }
...
//someService
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Override
    public void setMessageStatus(MessageDto messageDto, StatusEnum status) {
        MessageEntity messageEntity = messageRepository.getById(messageDto.getId());
        messageEntity.setStatus(status);
        messageEntity.setError(messageDto.getException().getMessage());
    }

and even though I set all the methods to @Transactional(propagation = Propagation.REQUIRES_NEW) I get UnexpectedRollbackException when trying to set ERROR status after catching exception.

org.springframework.transaction.UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only

Documentation says that not all Transaction managers are supported

Note: Actual transaction suspension will not work on out-of-the-box on all transaction managers. This in particular applies to JtaTransactionManager, which requires the javax.transaction.TransactionManager to be made available it to it (which is server-specific in standard J2EE).

And that's how I create transaction manager:

    @Bean
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
        return transactionManager;
    }

I want to persist error status regardless of what exceptions occur beforehand.

So my question here is how do I make UnexpectedRollbackException go away?

Could it be that JpaTransactionManager does not support REQUIRES_NEW option?

java

spring

rollback

transactionmanager

0 Answers

Your Answer

Accepted video resources