title: Quarkus Transactions on different thread
body: <p>I have a quarkus application with an async endpoint that creates an entity with default properties, starts a new thread within the request method and executes a long running job and then returns the entity as a response for the client to track.</p>
  <pre class="lang-java prettyprint-override"><code>@POST
  @Transactional
  public Response startJob(@NonNull JsonObject request) {
          // create my entity
          JobsRecord job = new JobsRecord();
          // set default properties
          job.setName(request.getString(&quot;name&quot;));
  
          // make persistent
          jobsRepository.persist(job);
          
          // start the long running job on a different thread
          Executor.execute(() -&gt; longRunning(job));
  
          return Response.accepted().entity(job).build();
      }
  </code></pre>
  <p>Additionally, the long running job will make updates to the entity as it runs and so it must also be transactional. However, the database entity just doesn't get updated.</p>
  <p>These are the issues I am facing:</p>
  <ol>
  <li>I get the following warnings:</li>
  </ol>
  <pre><code>ARJUNA012094: Commit of action id 0:ffffc0a80065:f2db:5ef4e1c7:0 invoked while multiple threads active within it.
  ARJUNA012107: CheckedAction::check - atomic action 0:ffffc0a80065:f2db:5ef4e1c7:0 commiting with 2 threads active!
  </code></pre>
  <p>Seems like something that should be avoided.</p>
  <ol start="2">
  <li><p>I tried using <code>@Transaction(value = TxType.REQUIRES_NEW)</code> to no avail.</p>
  </li>
  <li><p>I tried using the <a href="https://quarkus.io/guides/transaction#api-approach" rel="nofollow noreferrer">API Approach</a> instead of the <code>@Transactional</code> approach on <code>longRunning</code> as mentioned in the guide as follows:</p>
  </li>
  </ol>
  <pre class="lang-java prettyprint-override"><code>@Inject UserTransaction transaction;
  .
  .
  .
  try {
      transaction.begin();
      jobsRecord.setStatus(&quot;Complete&quot;);
      jobsRecord.setCompletedOn(new Timestamp(System.currentTimeMillis()));
      transaction.commit();
  } catch (Exception e) {
      e.printStackTrace();
      transaction.rollback();
  }
  </code></pre>
  <p>but then I get the errors: <code>ARJUNA016051: thread is already associated with a transaction!</code> and <code>ARJUNA016079: Transaction rollback status is:ActionStatus.COMMITTED</code></p>
  <ol start="4">
  <li><p>I tried both the declarative and API based methods again this time with <a href="https://quarkus.io/guides/context-propagation" rel="nofollow noreferrer">context propagation</a> enabled. But still no luck.</p>
  </li>
  <li><p>Finally, based on the third approach, I thought keeping the <code>@Transactional</code> on the Http request handler and leaving <code>longRunning</code> as is without declarative or API based transaction approaches would work. However the database still does not get updated.</p>
  </li>
  </ol>
  <p>Clearly I am misunderstanding how JTA and context propagation works (among other things).</p>
  <p>Is there a way (or even a design pattern) that allows me to update database entities asynchronously in a quarkus web application? Also why wouldn't any of the approaches I took have any effect?</p>
  <p>Using quarkus 1.4.1.Final with ext: [agroal, cdi, flyway, hibernate-orm, hibernate-orm-panache, hibernate-validator, kubernetes-client, mutiny, narayana-jta, rest-client, resteasy, resteasy-jackson, resteasy-mutiny, smallrye-context-propagation, smallrye-health, smallrye-openapi, swagger-ui]</p>