Spring Batch 5 - Don't want it to automatically create tables in database

31 views Asked by At

I am migrating from Spring Boot 2 to Spring Boot 3, and from Spring Batch 4 to Spring Batch 5. I don't want Spring Batch to create the BATCH_JOB_INSTANCE tables in my database.

@Autowired
private DataSource dataSource;
   
@Bean
@Primary
public PlatformTransactionManager transactionManager() {
     final JpaTransactionManager tm = new JpaTransactionManager();
     tm.setDataSource(dataSource);
     return tm;
}
@Bean(name = "jobRepository")
public JobRepository createJobRepository() throws Exception {
    JobRepositoryFactoryBean factoryBean = new JobRepositoryFactoryBean();
    factoryBean.setDataSource(dataSource);
    factoryBean.setTransactionManager(transactionManager());
    factoryBean.afterPropertiesSet();
    return factoryBean.getObject();
}
   
@Bean(name = "jobname")
public Job jobname (PlatformTransactionManager transactionManager) throws Exception {
    return new JobBuilder("jobname ", createJobRepository())
      .start(getAlerteReader(transactionManager))
      .next(getAlerteProcess(transactionManager))
      .build();
}

When I build the project, I get the error:

PreparedStatementCallback; bad SQL grammar [SELECT JOB_INSTANCE_ID, JOB_NAME
FROM BATCH_JOB_INSTANCE
WHERE JOB_NAME = ?
and JOB_KEY = ?]

Has anyone else encountered this problem?

I have not imported EnableBatchProcessing et not extends DefaultBatchConfiguration.

1

There are 1 answers

0
Mahmoud Ben Hassine On BEST ANSWER

The in-memory job repository was removed in Spring Batch 5. If you want to use the default JDBC-based job repository you need to provide a datasource with batch tables (either create the tables manually or let Spring Boot do it for you). Otherwise you need to provide a custom job repository.