I am trying to read about Transaction but I cannot understand how would I rollback a async method call, is it even possible to do so.
I am facing an issue where User can select either select manual or driverless car for example. So if he selects driverless, then manual config will be disabled and other way round. Consider, he is moving from driverless to manual, driverless config is switched off and I make call to another service to switch off the manual configuration (which takes place in an Async call). I have added @Async
for a reason as it is time consuming call.
example
// Caller
@Override
public void enableDriverlessDriving(DriverlessDriving driverlessConfig) {
validator.validate(driverlessConfig);
driverlessDao.update(driverlessConfig);
if(drivelessConfig.isEnabled()) {
manualService.onDrivelessEnabled(driverlessConfig.getUserId());
}
}
// Callee
@Override
@Aysnc
public void onDrivelessEnabled(int userId) {
.....
// retrofit rest call. Timeout for 30 secs
ManualConfig config = client.getManualConfiguration(userid,30);
config.isEnabled(false);
try {
// retrofit rest call. timeout for 30 secs
client.updateManualConfig(config, userId,30);
}catch(CheckedExceptions e) { // throwing checked exceptions here.
LOGGER.error(...)
return;
}
}
If there is an invoke error for rest call to disable manual config, the driverless config is switched on but manual config is not switched off, both are turned on.
Question:
Adding
@Transactional
to the caller and callee method work? - May be, but then it will affect the performance, there would be no use of@Async
in this case.Should caller method have
@Transactional
, but the@Async
method should have@Transactional(REQUIRES_NEW)
? Would having rollback on the caller method@Transaction
rollback the transaction for checked exception in callee method?
I am expecting a solution where I achieve the integrity of data without compromising on performance (I want @Async
to work in a way it works now)