Spring批处理非法状态(仅在竞争条件下发生)>;:作业执行已在运行

Spring批处理非法状态(仅在竞争条件下发生)>;:作业执行已在运行,spring,spring-boot,spring-batch,Spring,Spring Boot,Spring Batch,在我的Spring批处理应用程序中,我使用PostgreSQL作为作业存储库,并使用以下登录来重新启动未完成的作业: try { jobRegistry.register(new ReferenceJobFactory(documetPipelineJob)); List<String> jobs = jobExplorer.getJobNames(); for (String job : jobs) { Set<JobExecution&g

在我的Spring批处理应用程序中,我使用PostgreSQL作为作业存储库,并使用以下登录来重新启动未完成的作业:

try {
    jobRegistry.register(new ReferenceJobFactory(documetPipelineJob));

    List<String> jobs = jobExplorer.getJobNames();
    for (String job : jobs) {

    Set<JobExecution> jobExecutions = jobExplorer.findRunningJobExecutions(job);

    for (JobExecution jobExecution : jobExecutions) {

        jobExecution.setStatus(BatchStatus.STOPPED);
        jobExecution.setEndTime(new Date());
        jobRepository.update(jobExecution);

        Long jobExecutionId = jobExecution.getId();
        jobOperator.restart(jobExecutionId);
    }
    }
} catch (Exception e) {
    LOGGER.error(e.getMessage(), e);
}
试试看{
jobRegistry.register(新引用jobFactory(documetPipelineJob));
List jobs=jobExplorer.getJobNames();
for(字符串作业:作业){
设置jobExecutions=jobExplorer.findRunningJobExecutions(作业);
for(作业执行作业执行:作业执行){
jobExecution.setStatus(BatchStatus.STOPPED);
jobExecution.setEndTime(新日期());
jobRepository.update(作业执行);
Long jobExecutionId=jobExecution.getId();
jobOperator.restart(jobExecutionId);
}
}
}捕获(例外e){
LOGGER.error(e.getMessage(),e);
}
但此逻辑失败,出现以下异常:

2018-08-01 14:33:21.777错误32306---[main] c、 v.p.d.service.batch.BatchServiceImpl:非法状态(仅发生 在竞争条件下):作业执行已与一起运行 名称=documetPipelineJob和参数=

这里可能有什么问题,如何解决

已更新


看起来像
jobRepository.update(作业执行)不向数据库提交更改。如何正确提交对数据库的更改?顺便说一句-此逻辑在内存数据库中使用H2时运行良好。

为了解决此问题,我添加了
incrementer(new RunIdIncrementer())

我还扩展了重启逻辑,以停止运行以下步骤:

try {
    jobRegistry.register(new ReferenceJobFactory(documetPipelineJob));

    List<String> jobs = jobExplorer.getJobNames();
    for (String job : jobs) {

    Set<JobExecution> jobExecutions = jobExplorer.findRunningJobExecutions(job);

    for (JobExecution jobExecution : jobExecutions) {

        Collection<StepExecution> stepExecutions = jobExecution.getStepExecutions();
        for (StepExecution stepExecution : stepExecutions) {
        BatchStatus status = stepExecution.getStatus();
        if (status.isRunning() || status == BatchStatus.STOPPING) {
            stepExecution.setStatus(BatchStatus.STOPPED);
            stepExecution.setEndTime(new Date());
            jobRepository.update(stepExecution);
        }
        }

        jobExecution.setStatus(BatchStatus.STOPPED);
        jobExecution.setEndTime(new Date());
        jobRepository.update(jobExecution);

        Long jobExecutionId = jobExecution.getId();

        jobOperator.restart(jobExecutionId);
    }
    }
} catch (Exception e) {
    LOGGER.error(e.getMessage(), e);
}
try {
    jobRegistry.register(new ReferenceJobFactory(documetPipelineJob));

    List<String> jobs = jobExplorer.getJobNames();
    for (String job : jobs) {

    Set<JobExecution> jobExecutions = jobExplorer.findRunningJobExecutions(job);

    for (JobExecution jobExecution : jobExecutions) {

        Collection<StepExecution> stepExecutions = jobExecution.getStepExecutions();
        for (StepExecution stepExecution : stepExecutions) {
        BatchStatus status = stepExecution.getStatus();
        if (status.isRunning() || status == BatchStatus.STOPPING) {
            stepExecution.setStatus(BatchStatus.STOPPED);
            stepExecution.setEndTime(new Date());
            jobRepository.update(stepExecution);
        }
        }

        jobExecution.setStatus(BatchStatus.STOPPED);
        jobExecution.setEndTime(new Date());
        jobRepository.update(jobExecution);

        Long jobExecutionId = jobExecution.getId();

        jobOperator.restart(jobExecutionId);
    }
    }
} catch (Exception e) {
    LOGGER.error(e.getMessage(), e);
}
@Override
protected JobRepository createJobRepository() throws Exception {
    JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setDataSource(dataSource);
    factory.setTransactionManager(transactionManager);
    factory.setIsolationLevelForCreate("ISOLATION_READ_UNCOMMITTED");
    factory.afterPropertiesSet();
    return factory.getObject();
}