title: Use @Transactional on Spring Rest CompletableFuture result
body: <p>In my current web application, i use @RestController with CompletableFuture result for all services.</p>
  
  <p>Database operations are asynchronous (CompletableFuture methods), but i would like commit operations only before send result</p>
  
  <p>i would like to commit database modifications after --save-- asynchronous ended (--save-- is a list of future business)</p>
  
  <pre><code>@RestController
  public class MyController {
  ...
  
    @RequestMappping(...)
    public CompletableFuture&lt;ResponseEntity&lt;AnyResource&gt;&gt; service(...){
  
     CompletableFuture ...
     .thenCompose(--check--)
     .thenAsync(--save--)
     ...ect
     .thenApply(
        return ResponseEntity.ok().body(theResource);
      );
    }
  
  }
  </code></pre>
  
  <p>-> i've tried with @Transactional, but it doesn't work (commit at the method's end but async method partially or not executed</p>
  
  <p>-> Other way with programmatic : </p>
  
  <pre><code>@RequestMappping(...)
  public CompletableFuture&lt;ResponseEntity&lt;AnyResource&gt;&gt; service(...){
  
  DefaultTransactionDefinition def = new DefaultTransactionDefinition();
  // explicitly setting the transaction name is something that can only be done programmatically
  def.setName("SomeTxName");
  def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
  
  TransactionStatus status = this.platformTransactionManager.getTransaction(def);
  
  CompletableFuture ...
     .thenCompose(--check--)
     .thenAsync(--save--)
     ...ect
     .thenApply(
  
  this.platformTransactionManager.commit(status)
  
        return ResponseEntity.ok().body(theResource);
      );
  
  }
  </code></pre>
  
  <p>An error occured "Cannot deactivate transaction synchronization - not active", supposed because not the same thread.</p>
  
  <p>Is there a proper way, to use transactional with CompletableFuture ?</p>