Search code examples
springspring-batchdatasource

Spring Batch : use in memory database for spring batch metadata tables


I'm using spring batch for reading data from an Oracle database and writing results in a CSV file.

I also need to separate spring batch metadata tables from the oracle database and for this, I configure two different data-sources in my batch configuration (an in-memory database for the spring batch metadata).

Here is my code :

BatchConfiguration.java

@Configuration
@EnableBatchProcessing
@EnableScheduling
public class BatchConfiguration {

  @Autowired
  public JobBuilderFactory jobBuilderFactory;

  @Autowired
  public StepBuilderFactory stepBuilderFactory;    


  @Bean
  @Qualifier("businessDataSource")
  @ConfigurationProperties(prefix = "spring.business.datasource")
  public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
  }

  @Bean
  @Primary
  @Qualifier("metadataDataSource")
  @ConfigurationProperties(prefix = "spring.metadata.datasource")
  public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
  }
}

Then my itemReader bean looks like :

@Bean
public ItemReader<Person> reader(@Qualifier("businessDataSource") DataSource dataSource) {
  .....
}

When I run my Batch, everything is OK.

But when I try to add a JobLauncher and Job beans in my BatchApplication.java like this :

@Autowired
private JobLauncher jobLauncher;

@Autowired
private Job job;

The batch is trying to use the metadata database for my business database :

Caused by: org.springframework.jdbc.BadSqlGrammarException: Executing query; bad SQL grammar

Is there an issue in my code?

Thanks a lot


Solution

  • Finally I got a suolution

    My reader bean was :

    @Bean
    public ItemReader<Person> reader(@Qualifier("businessDataSource") DataSource dataSource) {
      databaseReader.setDataSource(datasource);
       ....
    }
    

    And I change it by (and now it's works)

    @Bean
      public ItemReader<Person> reader() {
        JdbcCursorItemReader<Person> databaseReader = new JdbcCursorItemReader<>();
        databaseReader.setDataSource(primaryDataSource());
        ....
      }