jyn
jyn

Reputation: 73

Spring Batch Jpa Repository Save not committing the data

I am using Spring Batch and JPA to process a Batch Job and perform updates. I am using the default Repository implementations.

And I am using a repository.save to save the modified object in the processor. Also,I don't have any @Transactional Annotation specified in the processor or writer.

I don't see any exceptions throughout. The selects happen fine.

Is there any setting like "setAutoCommit(true)" that I should be using for the JPA to save the data in the DB.

Here is my step,reader and writer config: Also, my config class is annotated with EnableBatchProcessing

@EnableBatchProcessing
public class UpgradeBatchConfiguration extends DefaultBatchConfigurer{
 @Autowired 
private PlatformTransactionManager transactionManager;

@Override
protected JobRepository createJobRepository() throws Exception {
    JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setDataSource(getdataSource());
    factory.setTransactionManager(transactionManager);
    factory.setTablePrefix("CFTES_OWNER.BATCH_");
    factory.afterPropertiesSet();
    return factory.getObject();
}

@Bean(name = "updateFilenetJobStep")
public Step jobStep(StepBuilderFactory stepBuilderFactory,
        @Qualifier("updateFileNetReader") RepositoryItemReader reader,
        @Qualifier("updateFileNetWriter") ItemWriter writer,
        @Qualifier("updateFileNetProcessor") ItemProcessor processor) {
    return stepBuilderFactory.get("jobStep").allowStartIfComplete(true).chunk(1).reader(reader).processor(processor)
            .writer(writer).transactionManager(transactionManager).build();

}

@Bean(name = "updateFileNetWriter")
public ItemWriter getItemWriter() {
    return new BatchItemWriter();
}

@Bean(name = "updateFileNetReader")
public RepositoryItemReader<Page<TermsAndConditionsErrorEntity>> getItemReader(
        TermsAndConditionsErrorRepository repository) {

    RepositoryItemReader<Page<TermsAndConditionsErrorEntity>> reader = new RepositoryItemReader<Page<TermsAndConditionsErrorEntity>>();
    reader.setRepository(repository);
    reader.setMethodName("findAll");
    HashMap<String, Direction> map = new HashMap<String, Direction>();
    map.put("transactionId", Direction.ASC);
    reader.setSort(map);

    return reader;
}
}

And in the writer this is what I am using Repository.save

repository.save(entity);

Upvotes: 2

Views: 3045

Answers (1)

jyn
jyn

Reputation: 73

I was able to solve this by injecting a JPATransactionManager throughout(the job repository, job, step etc) , instead of the Autowired PlatformTransactionManager.

Upvotes: 1

Related Questions