From b4835ef52ea5b628c2c8e83e91e66306ee3abff5 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Mon, 24 Feb 2025 08:39:36 +0100 Subject: [PATCH] Upgrade the job execution status when appropriate in MongoJobExecutionDao Before this commit, the mongo implementation of job execution DAO did not upgrade the status of the job execution when synchronizing the state with the database. This commit fixes the issue by upgrading the status when appropriate, similar to the jdbc implementation. Resolves #4760 --- .../core/repository/dao/MongoJobExecutionDao.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/MongoJobExecutionDao.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/MongoJobExecutionDao.java index 90d3326a9..da1d81ff7 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/MongoJobExecutionDao.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/MongoJobExecutionDao.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 the original author or authors. + * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,6 @@ import org.springframework.batch.core.repository.persistence.converter.JobInstan import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.query.Query; -import org.springframework.data.mongodb.core.query.Update; import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer; import static org.springframework.data.mongodb.core.query.Criteria.where; @@ -143,14 +142,13 @@ public class MongoJobExecutionDao implements JobExecutionDao { @Override public void synchronizeStatus(JobExecution jobExecution) { - Query query = query(where("jobExecutionId").is(jobExecution.getId())); - Update update = Update.update("status", jobExecution.getStatus()); + JobExecution currentJobExecution = getJobExecution(jobExecution.getId()); + if (currentJobExecution != null && currentJobExecution.getStatus().isGreaterThan(jobExecution.getStatus())) { + jobExecution.upgradeStatus(currentJobExecution.getStatus()); + } // TODO the contract mentions to update the version as well. Double check if this // is needed as the version is not used in the tests following the call sites of // synchronizeStatus - this.mongoOperations.updateFirst(query, update, - org.springframework.batch.core.repository.persistence.JobExecution.class, - JOB_EXECUTIONS_COLLECTION_NAME); } }