Please refer to the below snippet of code
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = { Throwable.class })
@Override
public MyClass myMethod(myParams) {
try{
myRepo.saveAll(myEntityList);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(myUrl,requestParam,String.class);
if(responseEnity.getBody()!=null && StringUtils.equalsIgnoreCase(responseEnity.getBody(),"Failure")){
log.error("Rest Call Failed");
throw new RestClientException("Error in remote service");
}
}catch(Exception e){
e.printStackTrace();
}
}
Now in the console log, I can see RestClientException
being thrown but myRepo.saveAll(myEntityList);
doesn't rollback instead it is inserting data into DB.
Can anyone figure out the issue?
My requirement is that in RestServiceCall
if I get a failure response then my current transaction should get rollback entirely.
CodePudding user response:
Your transaction is not rollbacked because there is no exception thrown, you're catching it.
As @Sumit Ghost mentioned, you can log it and rethrow the exception