0

I have an application scoped bean as follows

@ApplicationScoped
public class Worker {

   public void process(Long id) {
       final Runnable runnable = () -> {
           doATransaction(id);           
      };
      executor.execute(runnable);
    }
    
   @Transactional
    public void doATransaction(Long id) {
        User user  = User.findById(id);
    }
}

I am getting a javax.enterprise.context.ContextNotActiveException. I also tried adding the doATransaction() to another Bean, which was injected in this Worker, as suggested here. Still had the same problem.

Does anyone have an idea of what I could do next?

The Exception I am getting is

Exception in thread "pool-13-thread-3" javax.enterprise.context.ContextNotActiveException
    at io.quarkus.arc.impl.ClientProxies.getDelegate(ClientProxies.java:40)
    at io.quarkus.hibernate.orm.runtime.RequestScopedSessionHolder_ClientProxy.arc$delegate(RequestScopedSessionHolder_ClientProxy.zig:42)
    at io.quarkus.hibernate.orm.runtime.RequestScopedSessionHolder_ClientProxy.getOrCreateSession(RequestScopedSessionHolder_ClientProxy.zig:160)
    at io.quarkus.hibernate.orm.runtime.session.TransactionScopedSession.acquireSession(TransactionScopedSession.java:103)
    at io.quarkus.hibernate.orm.runtime.session.TransactionScopedSession.find(TransactionScopedSession.java:168)
    at io.quarkus.hibernate.orm.runtime.session.ForwardingSession.find(ForwardingSession.java:68)
    at io.quarkus.hibernate.orm.panache.common.runtime.AbstractJpaOperations.findById(AbstractJpaOperations.java:173)
    at com.example.User.findById(User.java)
bobby
  • 2,629
  • 5
  • 30
  • 56
  • Can you please add more details about the `ContextNotActiveException` you get? I suspect it's missing the request context, is that right? – Ladicek Aug 26 '21 at 06:49
  • @Ladicek I am not sure how I can check that. The exception I get is Exception in thread "pool-63-thread-2" javax.enterprise.context.ContextNotActiveException at io.quarkus.arc.impl.ClientProxies.getDelegate(ClientProxies.java:40) – bobby Aug 26 '21 at 09:16
  • 1
    Ouch, too bad, I thought the context name will be there :-/ Can you add more of the stacktrace? It might show what bean is being looked up when the exception happens, and consequently what context is inactive. I suspect it's a the request context, anyway, because the transactional things in Quarkus are request-scoped IIRC. Obviously the request context is not active on the new thread, unless you use `ManagedExecutor` from MP Context Propagation, but using that comes with constraints and shouldn't be done blindly. – Ladicek Aug 26 '21 at 15:42
  • @Ladicek have updated the question – bobby Aug 26 '21 at 19:08
  • I think that you execute the transactional method in a different thread than your request and that is why your context is not active. – Marek Żylicz Aug 26 '21 at 19:25
  • 1
    Think about using quarkus events https://quarkus.io/guides/reactive-event-bus – Marek Żylicz Aug 26 '21 at 19:29
  • 1
    And about context propagation in quarkus https://quarkus.io/guides/context-propagation – Marek Żylicz Aug 26 '21 at 19:49
  • 1
    OK, as I suspected, it's clearly the request context. So, here's a thing. Add a dependency on `quarkus-smallrye-context-propagation`, inject a `ManagedExecutor` and use that instead of your own `executor`. That should work, but note that you should _not_ have multiple threads accessing the same request-scoped objects. So this is really meant for cases where request processing sometimes has to hop from one thread to another and then perhaps back, but it's still sequential. If that's your case, it will be enough. – Ladicek Aug 27 '21 at 08:30
  • @Ladicek A bit of terminology clarification here. Is the request context, related to the scoped of the bean (RequestScoped/ApplicationScoped) etc – bobby Aug 27 '21 at 09:51
  • Yes, scopes and contexts are closely related. You can think of _context_ as a storage of objects of some _scope_. – Ladicek Aug 27 '21 at 13:08

0 Answers0