CompletableFuture vs Spring Transactions CompletableFuture vs Spring Transactions spring spring

CompletableFuture vs Spring Transactions


The reason of your problem is, as said above, that the transaction ends when the return of method process(..) is reached.

What you can do, is create the transaction manually, that gives you fullcontrol over when it starts and ends.

Remove @Transactional

Autowire the TransactionManager then in process(..) :

    TransactionDefinition txDef = new DefaultTransactionDefinition();    TransactionStatus txStatus = transactionManager.getTransaction(txDef);    try {    //do your stuff here like        doWhateverAsync().then(transactionManager.commit(txStatus);)    } catch (Exception e) {        transactionManager.rollback(txStatus);        throw e;    }


In case of Spring Boot Application , you need following configurations.

The main application method should be annotated with @EnableAsync.

@Async annotation should be on the top of method having @Transactional annotation. This is necessary to indicate processing will be taking place in child thread.