我是 Dropwizard 和 Hibernate 的新手。目前我正在開發一個應用程式,我需要使用 AsyncThreadPool 從我的主執行緒執行一些需要立即發送回應的耗時操作。在同步流中,操作作業正常,因為我的偵聽器有 @UnitOfWork 注釋。但是在執行緒池產生的執行緒中,我每次都會收到“當前沒有會話系結到執行背景關系”錯誤。我在執行緒類和 run() 方法上嘗試了 @UnitOfWork 注釋,因為檔案說我必須從訪問實體的資源中使用這個注釋。這是我的可運行類。
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);
}
在此先感謝您的幫助。
uj5u.com熱心網友回復:
確保您使用的是托管的 ExecutorService(即不要自己創建它,使用內置方法):
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();
}
}
uj5u.com熱心網友回復:
@UnitOfWork 使用當前會話背景關系策略,所以需要將會話放到當前執行緒背景關系中
Dropwizard @UnitOfWork 與異步資料庫呼叫
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/370634.html
