我使用Spring Batch StoredProcedureItemReader来检索结果集,并使用JpaItemWriter将其插入到另一个数据库中。下面是我的代码配置。
@Bean
public JdbcCursorItemReader jdbcCursorItemReader(){
JdbcCursorItemReader jdbcCursorItemReader = new JdbcCursorItemReader();
jdbcCursorItemReader.setSql("call myProcedure");
jdbcCursorItemReader.setRowMapper(new MyRowMapper());
jdbcCursorItemReader.setDataSource(myDataSource);
jdbcCursorItemReader.setFetchSize(50);
jdbcCursorItemReader.setVerifyCursorPosition(false);
jdbcCursorItemReader.setSaveState(false);
return jdbcCursorItemReader;
}
@Bean
public Step step() {
threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(50);
threadPoolTaskExecutor.setMaxPoolSize(100);
threadPoolTaskExecutor.setThreadNamePrefix("My-TaskExecutor ");
threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(Boolean.TRUE);
threadPoolTaskExecutor.initialize();
return stepBuilderFactory.get("myJob").transactionManager(secondaryTransactionManager)
.chunk(50).reader(jdbcCursorItemReader())
.writer(myJpaItemWriter())
.taskExecutor(threadPoolTaskExecutor)
.throttleLimit(100)
.build();
}代码在没有多线程或threadpooltaskexecutor.However的情况下运行良好,在使用它们时,我遇到了下面的错误。
Caused by: java.sql.SQLDataException: Current position is after the last row
could not execute statement [n/a] com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint我已经尝试过使用JdbcCursotItemReader,但我仍然面临着同样的error.Any建议,告诉我如何实现这一点
发布于 2019-01-08 05:12:51
JdbcCursorItemReader不是线程安全的,因为它所基于的ResultSet不是线程安全的。StoredProcedureItemReader也是基于ResultSet的,因此它也不是线程安全的。请参阅https://stackoverflow.com/a/53964556/5019386
尝试使用is thread-safe的JdbcPagingItemReader,或者如果您真的必须使用StoredProcedureItemReader,则通过将其包装在SynchronizedItemStreamReader中来使其线程安全。
希望这能有所帮助。
https://stackoverflow.com/questions/54075846
复制相似问题