1

I am creating a sample Spring Batch Application. Spring batch version is 2.2.7 java version is 1.7. In Application property file i am placing spring.batch.initialize-schema=never. I doesnt need the job meta data to be stored anywhere , in memory db or in my db. But when i run the application i am getting an error like below , how i can avoid this . Please help.

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT 
JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?]; nested 
exception is java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist


@Configuration
@EnableBatchProcessing
public class SpringConfig {

@Bean
public DataSource dataSource() {
    final DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
    dataSource.setUrl(url);
    dataSource.setUsername("root");
    dataSource.setPassword("root");
    return dataSource;
}
private JobRepository getJobRepository() throws Exception {
    JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setDataSource(dataSource());
    factory.setTransactionManager(getTransactionManager());
    factory.afterPropertiesSet();
    return (JobRepository) factory.getObject();
}

private PlatformTransactionManager getTransactionManager() {
    return new ResourcelessTransactionManager();
}

public JobLauncher getJobLauncher() throws Exception {
    SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
    jobLauncher.setJobRepository(getJobRepository());
    jobLauncher.afterPropertiesSet();
    return jobLauncher;
}

}

// trying to implement BatchConfigurer in my configuration class.But still it looking for Data source ( Error : DataSource must not be null)

public class BatchJobConfig  implements BatchConfigurer{ 

@Override
public JobRepository getJobRepository() throws Exception {
    JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setTransactionManager(getTransactionManager());
    factory.afterPropertiesSet();
    return (JobRepository) factory.getObject();
}

@Override
public PlatformTransactionManager getTransactionManager()  {
    return new ResourcelessTransactionManager();

}

@Override
public JobLauncher getJobLauncher() throws Exception {
    SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
    jobLauncher.setJobRepository(getJobRepository());
    jobLauncher.afterPropertiesSet();
    return jobLauncher;
}
VKP
  • 589
  • 1
  • 8
  • 34
  • may be i am not following your answer properly . Added some more piece of code trying to implement what you suggested . – VKP Jun 18 '20 at 21:08
  • Have you removed methods from `getJobRepository`, `getTransactionManager` and `getJobLauncher` from the `SpringConfig` class? Please update the question with the latest status of your code because now I see those methods duplicated in both `SpringConfig` class and `BatchJobConfig` class. You can make `SpringConfig` extend `DefaultBatchConfigurer` as well. – Mahmoud Ben Hassine Jun 19 '20 at 07:02

1 Answers1

4

When you use @EnableBatchProcessing, by default Spring Batch will use the datasource you defined in your application context for its meta-data. If you want to use the map based job repository , you need to implement BatchConfigurer and override getJobRepositoy.

You can find more details in the documentation here: https://docs.spring.io/spring-batch/docs/4.2.x/reference/html/job.html#javaConfig

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • 1
    added the new piece of code by trying to implement the BatchConfigurer , but i am still getting DS must not null . – VKP Jun 18 '20 at 21:06