r/SpringBoot • u/iamwisespirit • Jan 09 '26
Question @Transactional method
What happen when I run executorsrrvice inside @Transactional method what would you offer like this scenario
2
Upvotes
r/SpringBoot • u/iamwisespirit • Jan 09 '26
What happen when I run executorsrrvice inside @Transactional method what would you offer like this scenario
View all comments
Show parent comment
2
u/zattebij Jan 10 '26 edited Jan 10 '26
That by itself won't help;
CompletableFutureis only about state tracking and propagating resolved futures' values to the next callback in the pipeline. It still uses anExecutorServiceto actually run these callbacks (an explicit pool if specified in the Async versions of the functors, or a defaultcommonPoolif not specified) meaning you still cannot use entities loaded by the main thread inside such tasks, and you'd still need to merge or re-load them (both of which would still require the manual transaction management or calling a@Transactionalmethod from within the task), or pass unmanaged projections.You'd actually also not have solved the synchronization of these multiple transactions by using
CompletableFuture.allOf, since a failure in one of them does not automatically cancel the others (it only makes the resultant future fail with that error once they're all done; but the others will continue to run after the first one fails). Also, some methods may just throw an exception rather than returning a failed future, which you'd have to normalize to ensure you catch all errors. Of course it is possible to synchronize parallel transactions, but that would require custom code checking for errors and cancelling futures - just usingCompletableFuturedoes not automatically do it.Going even deeper: relying on
CompletableFutureto synchronize such transactions comes with several caveats:CompletableFuture.canceldoes not know anything about threads or tasks, it only resolves the future with aCancellationException, moving the pipeline ahead. So if you tack on anythenXXXfunctor to a future representing a task (obtaining a newCompletableFuture), or even convert such aFuturereturned bysubmit()(it does not return aCompletableFuture!), you lose the ability to stop the task represented by an upstream future. You'd need to keep a reference to the originalFuturefor the task to be able to cancel it.Futuresupport cancelling an ongoing task.FutureTaskdoes (which is the implementation you get from asubmit()to anExecutorServiceobtained viaExecutors.new*(Pool|Executor). But for exampleForkJoinTask(used withForkJoinPool) does not support interrupting running tasks. So you'd have to be really careful what kind of executor you run the concurrent transactional code in.InterruptedExceptionis a checked exception, and those (by default) do NOT trigger a rollback with@Transactional.If OP just want to start some fire-and-forget concurrent processing of independent entities, he could just submit a task for each entity (projection) that needs to be processed in a pool of appropriate size, with error logging inside each task, and forget about synchronizing them. And if OP just wants to wait for all these tasks to be done, then
CompletableFuture.allOfis fine. But if tasks need synchronization (e.g. if one fails, others also have to be rolled back) then it will be complex. Future-based logic can be used to do such kind of synchronizations, but you'll always need custom code, becauseCompletableFutureitself will not properly unwind other tasks if one fails, and canceling aCompletableFuturein such a pipeline will not cause the upstream task to be stopped.