Skip to content

Conversation

@SHINMH
Copy link

@SHINMH SHINMH commented Oct 16, 2025

📝 Description
This PR adds collection prefix support for MongoDB job repository, mirroring the existing tablePrefix functionality in JDBC job repository. This enables multi-application support, environment isolation, and easier management of MongoDB collections in Spring Batch deployments.

🎯 Motivation
Currently, MongoDB job repository uses hardcoded collection names (BATCH_JOB_INSTANCE, BATCH_JOB_EXECUTION, etc.), making it difficult to:

Run multiple Spring Batch applications against the same MongoDB database
Isolate different environments (dev, test, prod)
Manage collections with custom naming conventions

🔧 Changes
EnableMongoJobRepository: Add collectionPrefix parameter (defaults to "BATCH_")
MongoJobRepositoryFactoryBean: Add collection prefix support
MongoDB DAO classes: Support configurable collection names via constructor parameters
BatchRegistrar: Pass collection prefix from annotation to factory bean
Tests: Add integration tests for collection prefix functionality

💡 Usage Example

@EnableBatchProcessing
@EnableMongoJobRepository(collectionPrefix = "MY_APP_")
public class BatchConfiguration {
    // Configuration
}

This will create collections like:
MY_APP_JOB_INSTANCE
MY_APP_JOB_EXECUTION
MY_APP_STEP_EXECUTION

Closes: collectionPrefix for MongoDB Job repository #4980

@SHINMH SHINMH changed the title dd collection prefix support for MongoDB job repository MongoDB collection prefix support for MongoDB job repository Oct 16, 2025
- Add collectionPrefix parameter to EnableMongoJobRepository annotation
- Update MongoDB DAO classes to support configurable collection names
- Maintain backward compatibility with default "BATCH_" prefix

Signed-off-by: Myeongha Shin <sky95012@gmail.com>
@SHINMH SHINMH changed the title MongoDB collection prefix support for MongoDB job repository Collection prefix support for MongoDB job repository Oct 16, 2025
Copy link

@gyuyeol gyuyeol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! I've added some comments

Comment on lines +61 to +67
mongoTemplate.createCollection("BATCH_SEQUENCES");
mongoTemplate.getCollection("BATCH_SEQUENCES")
.insertOne(new Document(Map.of("_id", PREFIX + "JOB_INSTANCE_SEQ", "count", 0L)));
mongoTemplate.getCollection("BATCH_SEQUENCES")
.insertOne(new Document(Map.of("_id", PREFIX + "JOB_EXECUTION_SEQ", "count", 0L)));
mongoTemplate.getCollection("BATCH_SEQUENCES")
.insertOne(new Document(Map.of("_id", PREFIX + "STEP_EXECUTION_SEQ", "count", 0L)));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The BATCH_SEQUENCES collection should also have a prefix. Otherwise, batch jobs with different prefixes will end up sharing the same collection. I understand that it works correctly due to the prefix on the document ID. However, using the same collection will likely cause confusion.

Also, class MongoSequenceIncrementer needs to be modified to apply the prefix. This class also has the BATCH_SEQUENCES collection hardcoded.

Comment on lines +59 to +60
this.jobExecutionIncrementer = new MongoSequenceIncrementer(mongoOperations,
collectionPrefix + JOB_EXECUTIONS_SEQUENCE_NAME);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if we could pass the collectionPrefix as a parameter and use it internally as a prefix for both the collection name and the sequence ID.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

collectionPrefix for MongoDB Job repository

2 participants