Spring
Follow
MongoDB-backed Spring Batch jobs and more in Spring Boot 4.1
Spring Batch historically relied on SQL databases for its JobRepository to store job execution state. This requirement persisted even for users primarily working with NoSQL databases like MongoDB. Recent Spring Batch versions have decoupled the JobRepository from JDBC, and Spring Boot 4.1 introduces autoconfiguration for MongoDB. This allows batch metadata to be stored in MongoDB, eliminating the need for a separate SQL database. The article demonstrates a complete example of a Spring Batch job using MongoDB for its JobRepository. The example sets up MongoDB and PostgreSQL using Docker Compose. It defines an ETL job that reads customer data from CSV and writes it to a PostgreSQL table. This is achieved through a two-step job: one to reset the destination table and another for the actual data transfer. The data transfer step uses a FlatFileItemReader, a simple processor, and a JdbcBatchItemWriter. Fault tolerance and retry mechanisms are configured for this chunk-based step. The application is then run, with all job execution details being recorded in MongoDB. Observability is enhanced with OpenTelemetry and Grafana integration. The article also touches on enabling Spring Batch to work with GraalVM native images and introduces lazy data source connections for optimization. The key takeaway is that Spring Batch now seamlessly integrates with MongoDB, allowing developers to use their preferred database without compromise.