Home > OS >  Getting "No session currently bound to execution context" when using Threadpool Executor
Getting "No session currently bound to execution context" when using Threadpool Executor

Time:12-01

I am new to Dropwizard and Hibernate. Currently I am working on an application where I need to use AsyncThreadPool for performing some time consuming operations from my main thread which needs to send a response right away. In the synchromous flow the operations are working fine because my listener has @UnitOfWork annotation in place. But in the threadpool spawned thread I am getting "No session currently bound to execution context" error every time. I tried @UnitOfWork annotation on thread class as well as the run() method, as the documentation says that I have to use this annotation from the resource accessing the instance. Here is my runnable class.

public class CallbackTask implements Runnable{

    Snapshot baseSnapshot;
    ProcessorResponse processorResponse;
    Job job;
    ElasticSearchDAO esDao;
    OrchestratorJobService orchestratorJobService;
    Snapshot snapshot;
    RestoreHelper restoreHelper;
    String indexName;
    public CallbackTask(Snapshot baseSnapshot, ProcessorResponse processorResponse, Job job, ElasticSearchDAO esDao, OrchestratorJobService orchestratorJobService, Snapshot snapshot, RestoreHelper restoreHelper, String indexName) {
        this.baseSnapshot = baseSnapshot;
        this.processorResponse = processorResponse;
        this.job = job;
        this.esDao = esDao;
        this.orchestratorJobService = orchestratorJobService;
        this.snapshot = snapshot;
        this.restoreHelper = restoreHelper;
        this.indexName = indexName;
    }

    @Override
    @Transactional
    public void run() {
        int retryCount = job.getRetrialCount()==null ? 0: job.getRetrialCount();
        if(retryCount< JOB_RETRY_COUNT){
            job.setRetrialCount(  retryCount);
            //orchestratorJobService.runConfig(job.getConfigId(), null);
        }else{
            snapshot.setSoftDelete(true);
        }
        orchestratorJobService.persistData(job, snapshot);
    }

Thanks in advance for any help.

CodePudding user response:

Make sure that you're using a managed ExecutorService (i.e. do not create it yourself, use the built-in methods):

public class MyApplication extends Application<MyConfiguration> {
    @Override
    public void run(MyConfiguration configuration, Environment environment) {

        ExecutorService executorService = environment.lifecycle()
            .executorService(nameFormat)
            .maxThreads(maxThreads)
            .build();

        ScheduledExecutorService scheduledExecutorService = environment.lifecycle()
            .scheduledExecutorService(nameFormat)
            .build();
    }
}

CodePudding user response:

@UnitOfWork uses the current session context strategy, so you need to put a session to the current thread context

Dropwizard @UnitOfWork with asynchronous database call

  • Related