I've read the directives in the Spring @Transactional - isolation, propagation
but I don't understand how made this feature that I explain: I've an asynchronus method like this
@Transactional(timeout = TRANSACTION_TIMEOUT, propagation = Propagation.REQUIRES_NEW)
@Async("asyncImportExecutor")
public void startBeggin(Long id, String jwtToken) {
try {
. . .
} catch (Exception e) {
log.error("Error: " + e);
utilsService.setFlag(id, 0L); // every type of error, revert flag to 0
}
and the utilService method called on catch, is this
public void setFlag(Long id, Long state) {
/* recover data from repository */
Processo processo = getRepo().getOne(id);
processo.setAvanzamento(state);
getRepo().saveAndFlush(processo);
}
this method (setFlag) is called one time passing 1L before call the asynchronus method
getService().setFlag(id, (long) 1);
and at the end of all operations in asynchronus method passing zero
getService().setFlag(id, (long) 0);
but, if the asynchronus method fail for some problems, the call in the catch branch fail, because
org.springframework.orm.jpa.JpaSystemException: Transaction was marked for rollback only; cannot commit; nested exception is org.hibernate.TransactionException: Transaction was marked for rollback only; cannot commit
and the flag remain to 1.
So, I hope I was clear, but ... how can I make sure that, on any exception that occurs between the operations within the asynchronous method (startBeggin) after the rollback of that operations, set that value on that object?
How can I set (or update) a field on an object, on the roolback of a transaction (failed)?
The object impacted of the transactional method is the same (recovered by ID) but all the roolbacked operations don't are linked to that flag, that I want to set anyhow.
Thanks in advance to anyone who can give me a solution / idea.