0

I'm new to spring batch and I'm trying to learn about it. I implemented datasource and batch configuration but it doesn't seem to work, because

Exception in thread "main" 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: Table '$tableName.batch_job_instance' doesn't exist

DataSourceConfiguration class :

@Configuration
public class DataSourceConfiguration {
    @Bean
    public DataSource mysqlDataSource() throws SQLException {
        final SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource.setDriver(new com.mysql.cj.jdbc.Driver());
        dataSource.setUrl("jdbc:mysql://localhost:3306/task4");
        dataSource.setUsername("name");
        dataSource.setPassword("password");
        return dataSource;
    }
}

And I have Batch configuration class, but I dont know @EnableBatchProcessing annotation works without springboot

@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class BatchConfiguration {
    @Autowired
    private JobBuilderFactory jobs;
    @Autowired
    private StepBuilderFactory steps;
    @Autowired
    private DataSource dataSource;
    @Bean
    public ItemReader<User> itemReader() {
        return new JdbcCursorItemReaderBuilder<User>()
                .dataSource(this.dataSource)
                .name("creditReader")
                .sql("select userLogin, userEmail, userPassword, accountBalance from userInfo")
                .rowMapper(new UserRowMapper())
                .build();
    }
    @Bean
    public ItemWriter<User> simpleItemWriter(){
        return new SimpleUserWriter();
    }
    @Bean
    public Job sampleJob() {
        return this.jobs.get("sampleJob")
                .start(step1())
                .build();
    }
    @Bean
    public Step step1() {
        return this.steps.get("step1")
                .<User,User>chunk(10)
                .reader(itemReader())
                .writer(simpleItemWriter())
                .build();
    }
}

Main class contains those lines of code :

 public class Demo {
    public static void main(String[] args) throws SQLException {
        ApplicationContext context = new AnnotationConfigApplicationContext(BatchConfiguration.class);
        context.getBean("jobRepository");
        JobLauncher jobLauncher =  context.getBean("jobLauncher",JobLauncher.class);
        Job job =  context.getBean("sampleJob", Job.class);
        try {
            JobExecution execution = jobLauncher.run(job, new JobParameters());
            System.out.println("Job Exit Status : "+ execution.getStatus());

        } catch (JobExecutionException e) {
            System.out.println("Job ExamResult failed");
            e.printStackTrace();
        }
    }
}

How can I solve this problem?Thanks for helping

1 Answer 1

1

The error seems to say that you are missing the tables needed by Spring batch.

Check https://docs.spring.io/spring-batch/docs/4.3.x/reference/html/schema-appendix.html

You can also find SQL files ready for the different DB engine under org/springframework/bacth/core/schema-<DB>.sql in the spring batch core jar file

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.