33

I am new to Spring Batch. I have configured my job with inmemoryrepository. But still, it seems it is using DB to persist job Metadata. My spring batch Configuration is :

@Configuration
public class BatchConfiguration {
    
    
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    
    @Autowired
    private JobBuilderFactory jobBuilder;
    
    @Bean
    public JobLauncher jobLauncher() throws Exception {
        SimpleJobLauncher job =new SimpleJobLauncher();
        job.setJobRepository(getJobRepo());
        job.afterPropertiesSet();
        return job;
    }
    
    
    @Bean
    public PlatformTransactionManager getTransactionManager() {
        return new ResourcelessTransactionManager();
    }

    @Bean
    public JobRepository getJobRepo() throws Exception {
        return new MapJobRepositoryFactoryBean(getTransactionManager()).getObject();
    }
    

    
    
    @Bean
    public Step step1(JdbcBatchItemWriter<Person> writer) throws Exception {
        return stepBuilderFactory.get("step1")
            .<Person, Person> chunk(10)
            .reader(reader())
            .processor(processor())
            .writer(writer).repository(getJobRepo())
            .build();
    }
    
     @Bean
    public Job job( @Qualifier("step1") Step step1) throws Exception {
        return jobBuilder.get("myJob").start(step1).repository(getJobRepo()).build();
    }

}

How to resolve above issue?

1

7 Answers 7

72

If you are using Sprint boot a simple property in your application.properties will solve the issue spring.batch.initialize-schema=ALWAYS

5
  • The above solution is applicable for Spring Boot V2 and above. For spring boot versions prior to 2, you can use the following two (one is to drop the schema and one is to create it before each test) on your test class (in this case I am using a h2 in memory db for my tests, otherwise, look for the write script in the jar folder): \n @Sql("classpath:org/springframework/batch/core/schema-drop-h2.sql") @Sql("classpath:org/springframework/batch/core/schema-h2.sql")
    – Youness
    Mar 27, 2020 at 17:50
  • 15
    spring.batch.initialize-schema is deprecated in spring boot 2.5. Use spring.batch.jdbc.initialize-schema = ALWAYS instead Jun 20, 2021 at 13:23
  • I used the property as mentioned in the answer above, & it worked perfectly, not sure if @LucaPasini your comment might also be correct, but wanted to say that it works perfectly with the initial property in the answer. https://docs.spring.io/spring-boot/docs/2.1.x/reference/html/howto-database-initialization.html#howto-initialize-a-spring-batch-database Jan 11, 2022 at 10:36
  • IMPORTANT: if you've recently changed your lower_case_table_names setting, you might run into this error because it's not able to create the tables. SOLUTION: revert to your original lower_case_table_names value, clean up by dropping all spring batch tables, then change to lower_case_table_names=1 Apr 17, 2022 at 7:48
  • 1
    After I moved to Spring Boot 2.7.0 I had no problems, except the problem the question other has, reason was: spring.batch.initialize-schema is deprecated in spring boot 2.5. Use spring.batch.jdbc.initialize-schema = ALWAYS I did that, and it worked again. So, with the current Spring Boot version it isn't optional anymore.
    – Jan
    Jun 1, 2022 at 16:17
2

If You are using Spring 3.0, remove @EnableBatchProcessing annotation.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    May 1, 2023 at 1:12
1

For a non-Spring Boot setup:


This error shows up when a datasource bean is declared in the batch configuration. To workaround the problem I added an embedded datasource, since I didn't want to create those tables in the application database:

@Bean
public DataSource mysqlDataSource() {
  // create your application datasource here
}

@Bean
@Primary
public DataSource batchEmbeddedDatasource() {
    // in memory datasource required by spring batch
    EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
    return builder.setType(EmbeddedDatabaseType.H2)
            .addScript("classpath:schema-drop-h2.sql")
            .addScript("classpath:schema-h2.sql")
            .build();
}

The initialization scripts can be found inside the spring-batch-core-xxx.jar under org.springframework.batch.core package.
Note I used an in-memory database but the solution is valid also for other database systems.

0

Those who face the same problem with MySql database in CentOS(Most Unix based systems).

Table names are case-sensitive in Linux. Setting lower_case_table_names=1 has solved the problem.

Find official document here

0

For those using versions greater then spring-boot 2.5 this worked inside of application.properties

spring.batch.jdbc.initialize-schema = ALWAYS
0

This solved my case:

spring.batch.jdbc.initialize-schema=ALWAYS
0

It worked for me:

spring.batch.jdbc.initialize-schema=ALWAYS

I am using spring 3.x.x and spring batch 5.0.x

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.