SIMULATAN
SIMULATAN

Reputation: 959

Quarkus Reactive Websocket Hibernate Query RequestScoped context was not active

I'm trying to do reactive Hibernate Panache queries on a websocket message in quarkus, but unfortunately, the reactive call just fails (without errors!). I now tried to get the result directly via the .await().indefinitely() methods, and now I can actually see an error (surprisingly).

Exception in thread "Thread-752" java.util.concurrent.CompletionException: javax.enterprise.context.ContextNotActiveException: RequestScoped context was not active when trying to obtain a bean instance for a client proxy of PRODUCER_METHOD bean [class=io.quarkus.hibernate.reactive.runtime.ReactiveSessionProducer, id=404040404040]
    - you can activate the request context for a specific method using the @ActivateRequestContext interceptor binding

I tried to do what the error message says and annotate pretty much anything I can with @ActivateRequestContext, but it keeps showing this message. I even tried Panache.withTransaction (even though I only need to do a query and no updates) or using a injected Mutinity.SessionFactory, but it keeps failing.

My reproduction code:

@OnMessage
@ActivateRequestContext
public void onMessage(Session session, String message) {
   // the websocket thread can't be blocked
   new Thread(() -> {
      System.out.println(MyHibernateEntity.findAll().firstResult().await().indefinitely());
   }).start();
}

I even tried to put the query into it's own method annotated with @ActivateRequestContext, but that doesn't help either.

Upvotes: 2

Views: 1415

Answers (1)

Clement
Clement

Reputation: 3212

The request context is not propagated to your second thread. In addition, I would use the worker thread pool of Quarkus, to avoid creating an uncontrolled number of threads:

@Inject ExecutorService executor;

@OnMessage
public void onMessage(Session session, String message) {
  executor.submit(() -> find());
}

@ActivateRequestContext
void find() {
System.out.println(
  MyHibernateEntity.findAll().firstResult().await().indefinitely());
}

BTW, why are you using hibernate reactive in this case? If you need an await, I don't think it's a good match. You should use Hibernate classic.

Upvotes: 0

Related Questions