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?

Hayi
  • 6,972
  • 26
  • 80
  • 139
prakash kumar
  • 520
  • 1
  • 5
  • 16

6 Answers6

72

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

Nandhini
  • 798
  • 8
  • 13
  • 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 '20 at 17:50
  • 15
    ```spring.batch.initialize-schema``` is deprecated in spring boot 2.5. Use ```spring.batch.jdbc.initialize-schema = ALWAYS``` instead – Luca Pasini Jun 20 '21 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](https://docs.spring.io/spring-boot/docs/2.1.x/reference/html/howto-database-initialization.html#howto-initialize-a-spring-batch-database) – Hasintha Abeykoon Jan 11 '22 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` – coding-dude.com Apr 17 '22 at 07: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 01 '22 at 16:17
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.

lainatnavi
  • 1,453
  • 1
  • 14
  • 22
1

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

  • 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](/help/how-to-answer). – Community May 01 '23 at 01:12
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

Siva R
  • 427
  • 2
  • 8
  • 23
0

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

spring.batch.jdbc.initialize-schema = ALWAYS
shahidfoy
  • 1,951
  • 1
  • 17
  • 23
0

This solved my case:

spring.batch.jdbc.initialize-schema=ALWAYS
Abd Abughazaleh
  • 4,615
  • 3
  • 44
  • 53