Repository navigation
Description
Bug description
MongoJobInstanceDao has two issues in the current main branch:
1. getJobNames() violates the JobInstanceDao interface contract
The JobInstanceDao#getJobNames() contract states:
"Retrieve the names of all jobs for which job instances exist, sorted alphabetically."
The JDBC implementation correctly uses SELECT DISTINCT JOB_NAME FROM BATCH_JOB_INSTANCE ORDER BY JOB_NAME.
However, the MongoDB implementation loads all documents into memory without deduplication or sorting:
public List<String> getJobNames() {
return this.mongoOperations
.findAll(JobInstance.class, COLLECTION_NAME)
.stream()
.map(JobInstance::getJobName)
.toList();
}Three problems:
findAll()loads every document into memory — potential OOM in production with many job instances- No deduplication — returns duplicate job names (JDBC uses
DISTINCT) - No sorting — interface contract requires alphabetical order
2. getJobInstances(String, int, int) performs pagination in memory instead of delegating to MongoDB
The current implementation fetches all matching documents and then paginates in Java:
public List<JobInstance> getJobInstances(String jobName, int start, int count) {
List<...> jobInstances = this.mongoOperations
.find(query.with(Sort.by(sortOrder)), ...).stream().toList(); // loads ALL
if (jobInstances.size() <= start) {
return Collections.emptyList();
}
return jobInstances.subList(start, Math.min(jobInstances.size(), start + jobInstances.size()))
.stream()
.map(this.jobInstanceConverter::toJobInstance)
.limit(count)
.toList();
}Two problems:
- All documents loaded into memory — pagination should use MongoDB's
skip()andlimit()instead Math.min(jobInstances.size(), start + jobInstances.size())always evaluates tojobInstances.size()(sincestart >= 0), making the upper bound calculation ineffective
Environment
- Spring Batch: main branch (6.x)
- Java: 17+
- Database: MongoDB
Expected behavior
getJobNames()should return distinct, alphabetically sorted job names without loading all documents — consistent with the JDBC implementation and theJobInstanceDaointerface contractgetJobInstances()should delegate pagination to MongoDB usingskip()andlimit()instead of performing it in memory
Minimal Complete Reproducible example
The existing integration test MongoJobInstanceDaoIntegrationTests#testGetJobNames does not verify deduplication or sorting, so it does not catch the issue. The following test demonstrates the problem:
@Test
void testGetJobNamesShouldReturnDistinctAndSortedNames() {
// Create multiple instances of the same job
dao.createJobInstance("batchJob", new JobParameters());
dao.createJobInstance("batchJob", new JobParametersBuilder().addLong("id", 1L).toJobParameters());
dao.createJobInstance("alphaJob", new JobParameters());
List<String> jobNames = dao.getJobNames();
// Should be deduplicated
assertEquals(2, jobNames.size());
// Should be sorted alphabetically
assertEquals("alphaJob", jobNames.get(0));
assertEquals("batchJob", jobNames.get(1));
}
Activity
SHINMH commentedon Feb 7, 2026
Hi, I’m interested in working on this issue. Could I take this up?
combe4259 commentedon Feb 7, 2026
Hi @SHINMH , thanks for your interest
opened this issue because I'm currently working on the fix myself. I'll be submitting a Pull Request shortly.
Fix MongoJobInstanceDao to comply with JobInstanceDao contract
Fix MongoJobInstanceDao to comply with JobInstanceDao contract