How to read data from Oracle Db using JdbcCursorItemReader with Multi threading

跟風遠走 提交于 2019-12-12 01:26:21

问题


I had spring batch application and configured step like this:

ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(4);
taskExecutor.setMaxPoolSize(10);
taskExecutor.afterPropertiesSet();
return this.stepBuilderFactory.get("step1")
            .<Mymodel, Mymodel>chunk(2500)              
            .reader(reader())
            .writer(writer())
            .taskExecutor(taskExecutor)
            .build();

And reader like this:

@Bean
public JdbcCursorItemReader<Mymodel> reader() {
    JdbcCursorItemReader<Mymodel> reader = new JdbcCursorItemReader<Mymodel>();
    reader.setDataSource(dataSource);
    reader.setSql("select * from User");
    reader.setRowMapper(new BeanPropertyRowMapper<>(Mymodel.class));
    reader.setVerifyCursorPosition(false);
    return reader;
}

When I execute application , getting this error:

org.springframework.jdbc.UncategorizedSQLException: Attempt to process next row failed; uncategorized SQLException for SQL [select * from User]; SQL state [99999]; error code [17289]; Result set after last row; nested exception is java.sql.SQLException: Result set after last row

Can you please help me to solve this.


回答1:


The JdbcCursorItemReader is not thread-safe as it wraps a ResultSet object which is not thread safe.

You can use a JdbcPaginingItemReader which is thread safe and optionally configure the page size to match the chunk size so that each page is processed in the same thread.

More details in the following answer: https://stackoverflow.com/a/28724199/5019386.

Hope this helps.



来源:https://stackoverflow.com/questions/53939299/how-to-read-data-from-oracle-db-using-jdbccursoritemreader-with-multi-threading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!