I am getting error Table 'test.batch_job_instance' doesn't exist
Asked Answered
K

8

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?

Kelton answered 23/3, 2018 at 6:21 Comment(1)
solution that helped me in latest Spring Batch https://mcmap.net/q/452652/-spring-batch-postgres-error-relation-quot-batch_job_instance-quot-does-not-existHellen
W
72

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

Wurst answered 26/4, 2018 at 11:17 Comment(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")Cranage
spring.batch.initialize-schema is deprecated in spring boot 2.5. Use spring.batch.jdbc.initialize-schema = ALWAYS insteadSelection
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-databaseDimitris
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=1Pileous
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.Dorindadorine
C
2

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

Chao answered 24/4, 2023 at 18:33 Comment(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.Oder
T
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.

Tiossem answered 17/3, 2021 at 15:17 Comment(0)
S
1

It worked for me:

spring.batch.jdbc.initialize-schema=ALWAYS

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

Sinistrodextral answered 7/10, 2023 at 14:34 Comment(0)
U
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

Uranic answered 15/5, 2019 at 13:18 Comment(0)
G
0

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

spring.batch.jdbc.initialize-schema = ALWAYS
Galang answered 20/9, 2022 at 20:28 Comment(0)
P
0

This solved my case:

spring.batch.jdbc.initialize-schema=ALWAYS
Prosecutor answered 26/9, 2022 at 21:0 Comment(0)
S
0

use this line in application.properties file

spring.batch.jdbc.initialize-schema=ALWAYS

Selfdrive answered 29/6, 2024 at 17:47 Comment(1)
repeat the answer which has already existedAisne

© 2022 - 2025 — McMap. All rights reserved.