如何使用 JdbcCursorItemReader 和多线程从 Oracle Db 读取数据

2023-12-29

我有 Spring Batch 应用程序并配置了如下步骤:

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();

读者喜欢这样:

@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;
}

当我执行应用程序时,出现此错误:

org.springframework.jdbc.UncategorizedSQLException:尝试下一步处理 行失败; SQL 的未分类 SQLException [从用户中选择 *]; SQL 状态[99999];错误代码[17289];最后一行之后的结果集;嵌套的 异常是 java.sql.SQLException:最后一行之后的结果集

你能帮我解决这个问题吗?


The JdbcCursorItemReader is 不是线程安全的 http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/database/JdbcCursorItemReader.html因为它包裹着一个ResultSet非线程安全的对象。

您可以使用JdbcPagingItemReader which 是线程安全的 http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/database/JdbcPagingItemReader.html并可以选择配置页面大小以匹配块大小,以便每个页面都在同一线程中处理。

更多详细信息请参见以下答案:https://stackoverflow.com/a/28724199/5019386 https://stackoverflow.com/a/28724199/5019386.

希望这可以帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 JdbcCursorItemReader 和多线程从 Oracle Db 读取数据 的相关文章

随机推荐