1

I want to use a different datasource for spring batch and created the below configuration class and autowired my required datasource to this as per the documentation.

I am using spring boot(2.2.6) and spring batch version 4.2.1.RELEASE

@Configuration
public class CustomBatchConfigurer extends DefaultBatchConfigurer {
    @Autowired
    @Qualifier("oracleDataSource")
    private DataSource dataSource;
    @Autowired
    private PlatformTransactionManager transactionManager;

    @Override
    protected JobRepository createJobRepository() throws Exception {
        JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
        factory.setDataSource(dataSource);
        factory.setTransactionManager(transactionManager);
        factory.setIsolationLevelForCreate("ISOLATION_READ_COMMITTED");
        factory.setTablePrefix("MYDB.BATCH_");
        factory.setMaxVarCharLength(1000);
        factory.afterPropertiesSet();
        return factory.getObject();
    }
}

But when I start my application with this , it never applies the setTablePrefix and it always fails with table not found error.

I need to use above as I have two different datasource and I need spring batch to use my oracleDataSource bean.

If I disable non oracleDataSource bean and move the proprieties to application.properties, everything works fine.

Please guide on how this can be fixed. I saw a similar issue here where the user was complaining about same table not found issue after extending DefaultBatchConfigurer class Spring batch tables in a different schema

Shan S
  • 658
  • 5
  • 18
  • In your answer, you say `I found that the table prefix was getting reset while creating the jobExplorer [..] I think this should be mentioned in the spring-batch documentation`: No, the prefix is not reset or overridden. The `JobRepository` and `JobExplorer` are different components and have two different factory beans to create them, so you need to set the prefix on both factory beans as you showed in your answer. – Mahmoud Ben Hassine Jul 06 '20 at 09:54

1 Answers1

0

After debugging the spring-batch source code I found that the table prefix was getting reset while creating the jobExplorer. The fix that works is as below.

I think this should be mentioned in the spring-batch documentation as jobExplorer is commonly used and can reset the table prefix back to BATCH_ if not properly overridden as below.

   
@Configuration
public class CustomBatchConfigurer extends DefaultBatchConfigurer {

    @Autowired
    @Qualifier("oracleDataSource")
    private DataSource dataSource;
    @Autowired
    private PlatformTransactionManager transactionManager;
   
   @Override
    protected JobRepository createJobRepository() throws Exception {
        JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
        factory.setDataSource(dataSource);
        factory.setTransactionManager(transactionManager);
        factory.setIsolationLevelForCreate("ISOLATION_READ_COMMITTED");
        factory.setTablePrefix("MYDB.BATCH_");
        factory.setMaxVarCharLength(1000);
        factory.afterPropertiesSet();
        return factory.getObject();
    }

    @Override
    protected JobLauncher createJobLauncher() throws Exception {
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setJobRepository(createJobRepository());
        jobLauncher.afterPropertiesSet();
        return jobLauncher;
    }

    @Override
    protected JobExplorer createJobExplorer() throws Exception {
        JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean();
        jobExplorerFactoryBean.setDataSource(this.dataSource);
        jobExplorerFactoryBean.setTablePrefix("MYDB.BATCH_");
        jobExplorerFactoryBean.afterPropertiesSet();
        return jobExplorerFactoryBean.getObject();
    }
    }