Home > database >  Failed to initialize the reader while running Spring Batch while reading from sql Table
Failed to initialize the reader while running Spring Batch while reading from sql Table

Time:10-17

Failed to initialize the reader while running Spring Batch.

I am running spring batch and what I need to read the data from the table and print that’s it. Now while I am running I am getting the error in reader. I have couple of approach but both is getting giving some error.

Here is what I am trying in one of the approach.

@Configuration
@EnableBatchProcessing
public class BatchConfing {

    private MyRepo myRepo;

    @Autowired
    private JobBuilderFactory jbf;
    static String str = “Pass”;
    private static final String QUERY_FIND_STUDENTS =
            "select "  
                    “Stu_info”  
                    "FROM myTable "  
                    "where status = " str;

    @Autowired
    private DataSource dataSource;
    @Bean
    public ItemReader<StudentDTO> itemReader() {
    
     return new JdbcCursorItemReaderBuilder<StudentDTO>()
                .name("cursorItemReader")
                .dataSource(dataSource)
                .sql(QUERY_FIND_STUDENTS)
                .rowMapper(new BeanPropertyRowMapper<>(StudentDTO.class))
                .build();
    }

    @Bean
    public ItemWriter<StudentDTO> writer(){
        return value -> {
            System.out.println(value.toString());

        };
    }
    @Bean
    public JobExecutionListener listener() {
        return new ScheduelListener();
    }
    @Autowired
    private StepBuilderFactory sbf;

    @Bean
    public Step stepA() {
        return sbf.get("stepA")
                .<StudentDTO,StudentDTO>chunk(2)
                .reader(itemReader())
                .writer(writer())
                .build()
                ;
    }

    @Bean
    public Job jobA(){
        return jbf.get("jobA")
                .incrementer(new RunIdIncrementer())
                .listener(listener())
                .start(stepA())
                .build()
                ;
    }

}

Now I am getting Failed to initialize the reader. What am I missing here.

Stack Trace :

Job started at: Mon Oct 17 09:33:01 IST 2022
Status of the Job: STARTED
2022-10-17 09:33:01.641  INFO 72998 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [stepA]
2022-10-17 09:33:03.476 ERROR 72998 --- [           main] o.s.batch.core.step.AbstractStep         : Encountered an error executing step stepA in job jobA

org.springframework.batch.item.ItemStreamException: Failed to initialize the reader
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:153) ~[spring-batch-infrastructure-4.3.7.jar:4.3.7]
    at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:104) ~[spring-batch-infrastructure-4.3.7.jar:4.3.7]
    at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:311) ~[spring-batch-core-4.3.7.jar:4.3.7]
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:205) ~[spring-batch-core-4.3.7.jar:4.3.7]
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:152) ~[spring-batch-core-4.3.7.jar:4.3.7]
    at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:413) ~[spring-batch-core-4.3.7.jar:4.3.7]
    at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:136) ~[spring-batch-core-4.3.7.jar:4.3.7]
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:320) ~[spring-batch-core-4.3.7.jar:4.3.7]
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:149) ~[spring-batch-core-4.3.7.jar:4.3.7]
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) ~[spring-core-5.3.23.jar:5.3.23]
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:140) ~[spring-batch-core-4.3.7.jar:4.3.7]
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:577) ~[na:na]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) ~[spring-aop-5.3.23.jar:5.3.23]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) ~[spring-aop-5.3.23.jar:5.3.23]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.3.23.jar:5.3.23]
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:128) ~[spring-batch-core-4.3.7.jar:4.3.7]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.23.jar:5.3.23]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) ~[spring-aop-5.3.23.jar:5.3.23]
    at jdk.proxy2/jdk.proxy2.$Proxy95.run(Unknown Source) ~[na:na]
    at com..fsspringscheduler.batch.JobRunner.run(JobRunner.java:32) ~[classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:771) ~[spring-boot-2.7.4.jar:2.7.4]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:755) ~[spring-boot-2.7.4.jar:2.7.4]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.7.4.jar:2.7.4]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.4.jar:2.7.4]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.4.jar:2.7.4]
    at com..fsspringscheduler.FsSqsSchedulerApplication.main(FsSqsSchedulerApplication.java:11) ~[classes/:na]
Caused by: org.springframework.jdbc.BadSqlGrammarException: Executing query; bad SQL grammar [select Stu_info FROM myTable where status = PASS]; nested exception is java.sql.SQLSyntaxErrorException: Unknown column 'PASS' in 'where clause'
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:239) ~[spring-jdbc-5.3.23.jar:5.3.23]
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70) ~[spring-jdbc-5.3.23.jar:5.3.23]
    at org.springframework.batch.item.database.AbstractCursorItemReader.translateSqlException(AbstractCursorItemReader.java:226) ~[spring-batch-infrastructure-4.3.7.jar:4.3.7]
    at org.springframework.batch.item.database.JdbcCursorItemReader.openCursor(JdbcCursorItemReader.java:133) ~[spring-batch-infrastructure-4.3.7.jar:4.3.7]
    at org.springframework.batch.item.database.AbstractCursorItemReader.doOpen(AbstractCursorItemReader.java:453) ~[spring-batch-infrastructure-4.3.7.jar:4.3.7]
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:150) ~[spring-batch-infrastructure-4.3.7.jar:4.3.7]
    ... 26 common frames omitted
Caused by: java.sql.SQLSyntaxErrorException: Unknown column 'PASS' in 'where clause'
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) ~[mysql-connector-java-8.0.30.jar:8.0.30]
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.30.jar:8.0.30]
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:916) ~[mysql-connector-java-8.0.30.jar:8.0.30]
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:972) ~[mysql-connector-java-8.0.30.jar:8.0.30]
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52) ~[HikariCP-4.0.3.jar:na]
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java) ~[HikariCP-4.0.3.jar:na]
    at org.springframework.batch.item.database.JdbcCursorItemReader.openCursor(JdbcCursorItemReader.java:128) ~[spring-batch-infrastructure-4.3.7.jar:4.3.7]
    ... 28 common frames omitted

2022-10-17 09:33:03.584  INFO 72998 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [stepA] executed in 1s938ms
Job Ended at: Mon Oct 17 09:33:03 IST 2022
Status of the Job: FAILED
2022-10-17 09:33:04.000  INFO 72998 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=jobA]] completed with the following parameters: [{time=1665979379235}] and the following status: [FAILED] in 2s772ms
JOB Execution completed!
2022-10-17 09:33:04.128  INFO 72998 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2022-10-17 09:33:04.196  INFO 72998 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2022-10-17 09:33:04.294  INFO 72998 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
Disconnected from the target VM, address: '127.0.0.1:54693', transport: 'socket'

CodePudding user response:

Apparently from the stack trace, the SQL

select Stu_info FROM myTable where status = PASS

that you configure for QUERY_FIND_STUDENTS has some syntax error. You need to check it against your DB schema for troubleshooting.

For example, does the table myTable really has the column status ? Or is it simply because you miss the single quote around the PASS value in the where clause ?


Update : From David 's feedback, it turns out that the problem is really because missing the single quote around the value PASS. So problem solved after he fixes the SQL to :


select Stu_info FROM myTable where status = 'PASS'
  • Related