Trying to prevent rollback of inner transactions. if inner transactions response not SUCCESS then outer transactions should rollback but inner transaction should save data.
@Transactional(rollbackFor=Exception.class, propagation=Propagation.REQUIRES_NEW)
private void test1(Account account) throws Exception {
DOA.save(account);
status = test2(account);
if(status!='SUCCESS'){
throw new Exception("api call failed");
}
}
@Transactional(propagation=Propagation.MANDATORY)
private void test2(Account account) {
response //API Call
DOA.save(response);
return response.status;
}
Configure the inner transactional method as Propagation.REQUIRES_NEW
such that it will always commit (i.e save the data) when the method completes whether the outer transactional method rollbacks or not.
Also, make sure outer method does not self invocation the inner method as @Transactional
does not work in this case (See Method visibility and @Transactional section in docs).
They should reside in different beans which the outer method calls the bean of the inner method:
@Service
public class Service1 {
@Autowired
private Service2 service2;
@Transactional(rollbackFor=Exception.class)
public void test1(Account account) throws Exception {
DOA.save(account);
status = service2.test2(account);
if(status!='SUCCESS'){
throw new Exception("Api call failed");
}
}
}
@Service
public class Service2{
@Transactional(propagation=Propagation.REQUIRES_NEW)
public void test2(Account account) {
response // API Call
DOA.save(response);
return response.status;
}
}