Skip to content

MongoJobInstanceDao: getJobNames() violates interface contract and getJobInstances() performs pagination in memory #5274

Closed

Description

@combe4259

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() and limit() instead
  • Math.min(jobInstances.size(), start + jobInstances.size()) always evaluates to jobInstances.size() (since start >= 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 the JobInstanceDao interface contract
  • getJobInstances() should delegate pagination to MongoDB using skip() and limit() 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

  1. added
    status: waiting-for-triage
    on Feb 7, 2026
  2. SHINMH commented on Feb 7, 2026

    @SHINMH
    Contributor

    Hi, I’m interested in working on this issue. Could I take this up?

  3. combe4259 commented on Feb 7, 2026

    @combe4259
    ContributorAuthor

    Hi, I’m interested in working on this issue. Could I take this up?

    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.

  4. added a commit that references this issue on Feb 7, 2026
    d8a6b6e
  5. added and removed
    status: waiting-for-triage
    on May 29, 2026
  6. added this to the 6.0.4 milestone on Jun 1, 2026
  7. added a commit that references this issue on Jun 1, 2026
    518611c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions