1

I need to write a method which is being called from a open transaction & will never rollback, even though exception occurs in the system. I used Propagation.NEVER to achieve this, Is it fine, or I should use PROPAGATION_NOT_SUPPORTED?

@Autowired
private PartnerTransactionService partnerTransactionService;

@PostConstruct
@Transactional
private void test(AuditDTO<Object> audit){
       partnerTransactionService.saveAudits(audit);
       partnerTransactionService.nonTrasactionsalSaveAudits(audit);
       throw new NullPointerException();
}

In some other service class(both are spring managed bean ie sprinf proxy is being created for both)-

public <T> void saveAudits(AuditDTO<T> audit){
        if(audit!=null){
            PartnerTransaction partnerTransaction=new PartnerTransaction(audit);
            partnerTransactionRepository.save(partnerTransaction);
        }
}

@Transactional(propagation = Propagation.NEVER)
public <T> void nonTrasactionsalSaveAudits(AuditDTO<T> audit){
   if(audit!=null){
       PartnerTransaction partnerTransaction=new PartnerTransaction(audit);
       partnerTransactionRepository.save(partnerTransaction);
      }     
}

I throws a NPE to check my code , I found both the data being save irrespective of exception, non being rollback, what I am missing?

TheCurious
  • 593
  • 1
  • 4
  • 29
  • If you call a method, annotated Transaction with propagation never from an existing transaction, it will throw an exception. Propagation.Never means no transaction allowed on that context. Are your objects beans or you create them with new keyword? It seems spring not wrap them with transactional proxy. Does the Transaction management enabled? – zlaval May 17 '20 at 20:03
  • should I use PROPAGATION_NOT_SUPPORTED instead `Propagation.NEVER`? Transaction management enabled-yes, & both are spring bean. I am testing with @PostConstruct is this the reason of not being rollback. – TheCurious May 18 '20 at 00:38

0 Answers0