Spring batch 5.x, not able to create the metadata tables in database (sqlserver)

206 Views Asked by At

Using spring batch5 and boot 3.x and java17, I'm trying to start a batch job and its failing while querying the table which is not created while bootstrapping of the application.

2023-11-01 09:20:07.566 ERROR [http-nio-9344-exec-2] ERROR - Caught generic exception org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [SELECT JOB_INSTANCE_ID, JOB_NAME FROM BATCH_JOB_INSTANCE WHERE JOB_NAME = ? and JOB_KEY = ?]; SQL state [S0002]; error code [208]; Invalid object name 'BATCH_JOB_INSTANCE'. at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1581)

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'BATCH_JOB_INSTANCE'. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:262)

I tried with the below configuration classes and properties. I'm expecting that how to make the spring batch meta data tables to be created automatically.

@Configuration
public class CollateralBatchConfiguration {

    @Bean
    public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor(JobRepository jobRepository,
            JobRegistry jobRegistry) {
        JobRegistryBeanPostProcessor postProcessor = new JobRegistryBeanPostProcessor();
        postProcessor.setJobRegistry(jobRegistry);
        return postProcessor;
    }
     

    @Bean
    public TaskExecutorJobLauncher simpleJobLauncher(JobRepository jobRepository) {
        TaskExecutorJobLauncher jobLauncher = new TaskExecutorJobLauncher();
        jobLauncher.setJobRepository(jobRepository);
        jobLauncher.setTaskExecutor(simpleAsyncTaskExecutor());
        return jobLauncher;
    }

    @Bean
    public JobOperator jobOperator(JobRepository jobRepository, JobExplorer jobExplorer, JobRegistry jobRegistry) {
        SimpleJobOperator jobOperator = new SimpleJobOperator();
        jobOperator.setJobLauncher(simpleJobLauncher(jobRepository));
        jobOperator.setJobExplorer(jobExplorer);
        jobOperator.setJobRegistry(jobRegistry);
        jobOperator.setJobRepository(jobRepository);
        return jobOperator;
    }

    private SimpleAsyncTaskExecutor simpleAsyncTaskExecutor() {
        SimpleAsyncTaskExecutor simpleAsyncTaskExecutor = new SimpleAsyncTaskExecutor(
                CollateralBatchConstants.EXECUTOR_THREAD_NAME_PREFIX);
        simpleAsyncTaskExecutor.setConcurrencyLimit(1);
        return simpleAsyncTaskExecutor;
    }

}
@Configuration
public class CollateralJobConfiguration {

    @Autowired
    private AccountStatusReader accountStatusReader;

    @Autowired
    private AccountStatusProcessor accountStatusProcessor;

    @Autowired
    private AccountStatusWriter accountStatusWriter;

    @Bean
    public Job collateralBatchJob(JobRepository jobRepository, Step odiJobStatusStep, Step collateralBatchStep) {
        return new JobBuilder(CollateralBatchConstants.COLLATERAL_BATCH_JOB, jobRepository)
                .start(odiJobStatusStep).next(collateralBatchStep).build();
    }

    @Bean
    public Step odiJobStatusStep(JobRepository jobRepository, Tasklet tasklet,
            PlatformTransactionManager transactionManager) {
        return new StepBuilder(CollateralBatchConstants.ODI_JOB_STEP, jobRepository)
                .tasklet(tasklet, transactionManager).build();
    }

    @Bean
    public Step collateralBatchStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
        return new StepBuilder(CollateralBatchConstants.COLLATERAL_BATCH_STEP, jobRepository)
                .<CollateralStagingLoanAccountStatus, AccountStatusUpdateResponse>chunk(1000, transactionManager)
                .reader(accountStatusReader).processor(accountStatusProcessor).writer(accountStatusWriter).build();
    }

}

With the above code I'm starting the batch job with the below code.

Long jobId = jobOperator.start(collateralBatchJob.getName(), props);

I'm trying with below properties.

spring.batch.initialize-schema=ALWAYS
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.database-platform=org.hibernate.dialect.SQLServerDialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=update

It's trying to query the database and with the table which is 'BATCH_JOB_INSTANCE' and not available.

How to fix it.

1

There are 1 best solutions below

3
On

@EnableBatchProcessing is now discouraged,remove that and try.