title: CompletableFuture vs Spring Transactions
body: <p><strong>Idea</strong></p>
  
  <p>I have a processing method which takes in a list of items and processes them asynchronously using external web service. The process steps also persist data while processing. At the end of whole process, I want to persist the whole process along with each processed results as well.</p>
  
  <p><strong>Problem</strong></p>
  
  <p>I convert each item in the list into <code>CompletableFuture</code> and run a processing task on them, and put them back into an array of futures. Now using its <code>.ofAll</code> method (in sequence method) to complete future when all the submitted tasks are completed and return another <code>CompletableFuture</code> which holds the result.</p>
  
  <p>When I want to get that result, I call <code>.whenComplete(..)</code>, and would want to set the returned result into my entity as data, and then persist to the database, however the repository save call just does nothing and continues threads just continue running, it's not going past the repository save call.</p>
  
  <pre><code> @Transactional
   public void process(List&lt;Item&gt; items) {
     List&lt;Item&gt; savedItems = itemRepository.save(items);
  
     final Process process = createNewProcess();
  
     final List&lt;CompletableFuture&lt;ProcessData&gt;&gt; futures = savedItems.stream()
       .map(item -&gt; CompletableFuture.supplyAsync(() -&gt; doProcess(item, process), executor))
       .collect(Collectors.toList());
  
     sequence(futures).whenComplete((data, throwable) -&gt; {
       process.setData(data);
       processRepository.save(process); // &lt;-- transaction lost?
       log.debug("Process DONE"); // &lt;-- never reached
     });
    }
  </code></pre>
  
  <p>Sequence method</p>
  
  <pre><code> private static &lt;T&gt; CompletableFuture&lt;List&lt;T&gt;&gt; sequence(List&lt;CompletableFuture&lt;T&gt;&gt; futures) {
      CompletableFuture&lt;Void&gt; allDoneFuture =
        CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]));
      return allDoneFuture.thenApply(v -&gt;
        futures.stream().map(CompletableFuture::join).collect(Collectors.toList())
      );
    }
  </code></pre>
  
  <p>What is happening? Why is the persist call not passing. Is the thread that started the transaction not able to commit the transaction or where does it get lost? All the processed data returns fine and is all good. I've tried different transaction strategies, but how is it possible to control which thread is gonna finish the transaction, if it's the case?</p>
  
  <p>Any advice?</p>