简体   繁体   中英

Spring Batch chunk size creating duplicates

I'm running a Spring batch job and whenever I increase the chunk size to anything greater than 1, lets say chunk size = n, it just duplicates the "nth" record "n" times instead of adding the n records. Below is my configuration, any pointers please?

@Bean
public JobLauncher jobLauncher(JobRepository jobRepository){
    SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
    jobLauncher.setJobRepository(jobRepository);
    return jobLauncher;
}


@Bean
public JobRepository jobRepository(DataSource dataSource, PlatformTransactionManager transactionManager) throws Exception{
    JobRepositoryFactoryBean factoryBean = new JobRepositoryFactoryBean();
    factoryBean.setDataSource(dataSource);
    factoryBean.setTransactionManager(transactionManager);
    factoryBean.setDatabaseType("mysql");
    return factoryBean.getObject();

}


@Bean
public PlatformTransactionManager transactionManager() {
    return new ResourcelessTransactionManager();
}

@Bean
public DataSource dataSource(){
    DriverManagerDataSource datasource = new DriverManagerDataSource();
    datasource.setDriverClassName("com.mysql.jdbc.Driver");
    datasource.setUrl("jdbc:mysql://localhost:3306/mysqltest?useSSL=false");
    datasource.setUsername("user");
    datasource.setPassword("pwd");
    return datasource;
}


@Bean
@Autowired
public Job projectMasterJob(JobBuilderFactory jobs, Step projectMasterDump) {
    return jobs.get("projectMasterJob").incrementer(new RunIdIncrementer())
            .flow(projectMasterDump).end().build();
}


@Bean
@Autowired
public Step projectMasterDump(StepBuilderFactory stepBuilderFactory,
                    FlatFileItemReader projectMasterReader, ItemWriter projectMasterDbWriter) {
    return stepBuilderFactory.get("projectMasterDump")
            .chunk(10).reader(projectMasterReader).writer(projectMasterDbWriter).build();
}

@Bean
@Autowired
public ItemWriter projectMasterDbWriter(DataSource dataSource){
    JdbcBatchItemWriter<ProjectMasterDTO> writer = new JdbcBatchItemWriter<ProjectMasterDTO>();
    writer.setSql(sql);
    writer.setDataSource(dataSource);
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
    return writer;
}

@Bean
@Autowired
public FlatFileItemReader projectMasterReader(ProjectMasterDTO projectMasterDTO, DataFormatter dataFormatter){
    ExcelItemReader<ProjectMasterDTO> reader = new ExcelItemReader<>();
    reader.setDataFormatter(dataFormatter);
    reader.setBean(projectMasterDTO);
    reader.setMapperFile(new File("../instance-config/projectMapper.json"));
    return reader;
}


@Bean
@Scope("prototype")
public ProjectMasterDTO projectMasterDTO(){
    return new ProjectMasterDTO();
}

In the above code, I've accidentally set the prototype bean to my singleton reader. So there's just one bean and it's being reused, because of which when a chunk process happens, the same bean is being overwritten and all "n" objects in the collection point to the same bean.

Retrieving a fresh bean each time inside the reader or creating a new instance resolves the issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM