I have a scenario in my project where I need to develop a asynchronous service which will perform some DB update operation from Spring Boot.I am aware that @Async always spawn a new thread.So,I am taking following actions:
I have created two separate Service classes.
First Service class [say Service Class A]
@Async
@Override
public Future<MyResponseModel> myMethodForApproval(parameterList) {
MyResponseModel myResponseModel= new MyResponseModel();
serviceClassB.performApproval(parameterList);
myResponseModel.setStatus("Success");
myResponseModel.setMessage("Approval process initiated successfully");
return new AsyncResult<MyResponseModel>(myResponseModel);
}
In Second service class [Say Service Class B, actual business logic is written]
@Transactional(readOnly = false, propagation = REQUIRES_NEW, rollbackFor = { Throwable.class })
public void performApproval(parameterList) {
//all the business logic checking.
repoLayer.saveAll(myEntities);
}
In controller Layer:
@RequestMapping(value = "/approveProcess", method = RequestMethod.POST, consumes = { "application/json" })
public ResponseEntity<MyResponseModel> approveProcess(@RequestBody parameterList) {
serviceClassA.myMethodForApproval(parameterList);
}
Now exception I am getting
org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress