java-ee-7

Java EE 7 - will new threads join ongoing transaction and commit/rollback at end of starting thread?


  doStuff(tasks);
 ...

@Transactional
public void doStuff(List<Task> tasks) {
   // run async
   for(... task ...)
     new Thread(() -> localServiceB.handle(task)).start();
}
....

@Transactional
public class LocalServiceB {
  public void handle() ....
}

? when will the outer transaction end?
? cand "handle() join the transaction from "doStuff"?
? how can a single transaction span the doStuff and all forked threads until the last "handle" has finished, even if "doStuff" has finished already. (hint: I prefer doStuff NOT to wait for the threads)


Solution

  • All tasks run without an explicit transaction (they do not enlist in the application component's transaction).

    https://docs.oracle.com/javaee/7/api/javax/enterprise/concurrent/ManagedExecutorService.html