Sergey Kochetov
Sergey Kochetov

Reputation: 21

Quarkus: ContextNotActiveException when i call db from verticle

When I try to call the DB from a vertex, I get a ContextNotActiveException. When called via Rest router everything works.

deploy Verticle:

@Inject
Vertx vertx;

void onStart(@Observes StartupEvent startupEvent) {
    DeploymentOptions deploymentOptions = new DeploymentOptions();
    deploymentOptions.setWorker(true);
    deploymentOptions.setWorkerPoolSize(DEFAULT_WORKER_POOL_SIZE);

    vertx.deployVerticle(new FileFinderForSaveWorker(), deploymentOptions);
}

my Verticle:

public class FileFinderForSaveWorker extends AbstractVerticle {

 @Override
 public void start(Promise<Void> startPromise) throws Exception {

    try {

        List<Media> mediaList = new MediaRepository().listAll();

    } catch (Exception e) {
        e.printStackTrace();
    }

 }
}

An exception occurs on this line:

List<Media> mediaList = new MediaRepository().listAll();

javax.enterprise.context.ContextNotActiveException: interface javax.enterprise.context.RequestScoped at io.quarkus.hibernate.orm.runtime.RequestScopedEntityManagerHolder_ClientProxy.arc$delegate(RequestScopedEntityManagerHolder_ClientProxy.zig:83) at io.quarkus.hibernate.orm.runtime.RequestScopedEntityManagerHolder_ClientProxy.getOrCreateEntityManager(RequestScopedEntityManagerHolder_ClientProxy.zig:191) at io.quarkus.hibernate.orm.runtime.entitymanager.TransactionScopedEntityManager.getEntityManager(TransactionScopedEntityManager.java:78) at io.quarkus.hibernate.orm.runtime.entitymanager.TransactionScopedEntityManager.createQuery(TransactionScopedEntityManager.java:317) at io.quarkus.hibernate.orm.runtime.entitymanager.ForwardingEntityManager.createQuery(ForwardingEntityManager.java:142) at io.quarkus.hibernate.orm.panache.runtime.JpaOperations.findAll(JpaOperations.java:328) at io.quarkus.hibernate.orm.panache.runtime.JpaOperations.listAll(JpaOperations.java:340) at ru.npc.sapsan.domain.repository.MediaRepository.listAll(MediaRepository.java) at ru.npc.sapsan.core.worker.FileFinderForSaveWorker.start(FileFinderForSaveWorker.java:18) at io.vertx.core.impl.DeploymentManager.lambda$doDeploy$9(DeploymentManager.java:556) at io.vertx.core.impl.ContextImpl.executeTask(ContextImpl.java:369) at io.vertx.core.impl.WorkerContext.lambda$wrapTask$0(WorkerContext.java:35) at io.vertx.core.impl.TaskQueue.run(TaskQueue.java:76) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.lang.Thread.run(Thread.java:748)

I thought that context-propagation should help me, but it doesn't work.

My dependencies in build.gradle:

    dependencies {
      annotationProcessor 'org.projectlombok:lombok:1.18.10'
      compileOnly 'org.projectlombok:lombok:1.18.10'
      implementation 'io.quarkus:quarkus-smallrye-reactive-streams-operators'
      implementation 'io.quarkus:quarkus-smallrye-context-propagation-parent:1.2.1.Final'
      implementation 'io.quarkus:quarkus-jdbc-postgresql'
      implementation 'io.quarkus:quarkus-hibernate-orm-panache'
      implementation 'io.quarkus:quarkus-tika'
      implementation 'io.quarkus:quarkus-vertx-web'
      implementation 'io.quarkus:quarkus-smallrye-openapi'
      implementation 'io.quarkus:quarkus-vertx'
      implementation 'io.quarkus:quarkus-flyway'
      implementation 'io.quarkus:quarkus-hibernate-orm'
      implementation 'org.projectlombok:lombok:1.18.10'
      implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
      implementation 'io.quarkus:quarkus-resteasy'
      implementation 'org.xerial.snappy:snappy-java:1.1.7.3'
      implementation 'org.reflections:reflections:0.9.11'

      testImplementation 'io.quarkus:quarkus-junit5'
      testImplementation 'io.rest-assured:rest-assured'
     }

I use hibernate-orm-panache

Repository:

 @ApplicationScoped
 public class MediaRepository implements PanacheRepository<Media> {
 }

Entity:

@Data
@Entity
@EqualsAndHashCode(callSuper = false)
@Table(name = "media")
public class Media extends PanacheEntity {

   @SerializedName("uuid")
   @Column(name = "uuid", unique = true, nullable = false, columnDefinition = "varchar(36)", length = 36, updatable = false)
   private String uuid = "";

}

Regards.

Upvotes: 2

Views: 5747

Answers (1)

iabughosh
iabughosh

Reputation: 551

You are using Panache which is behind the scene is a managed JPA/Hibernate which needs CDI Context to function probably. That's why it works fine with you when you call it through rest because CDI is already initiated. If you want to use vert.x routes with Quarkus, I suggest you to use Quarkus reactive-routes with context-propagation - which you are already configured - to achieve your goal.

Upvotes: 1

Related Questions