user6408869
user6408869

Reputation:

Read data from DB in Tasklet oriented Batch process

I have a simple question. I have to code a Tasklet oriented Spring Batch project which have to retrieve some data from DB, process info. and write it into a .json file. I´m using Spring Data JPA, but is this the correct and safest way to do this? If not, what is the best way to code this? Thanks a lot for you help!

Latest Tasklet reader code:

public class DataReader implements Tasklet, StepExecutionListener {

@Autowired
EntityRepository entityRepository;

@Autowired
ProductRepository productRepository;

@Autowired
SuscriptionRepository suscriptionRepository;

@Autowired
MapperUtils mapperUtils;

private List<EntityDTO> entityDataDTO;

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {        
    this.entityDataDTO = new ArrayList<EntityDTO>();
    List<Entidad> entities = entityRepository.findAll();

    for (Entidad entity : entities) {
        List<SuscriptionDTO> suscriptionsDTO = new ArrayList<SuscriptionDTO>();

        for (Suscripcion suscription : entity.getSuscripciones()) {
            List<Suscripcion> suscriptionsByProduct = suscriptionRepository.findSuscriptionsByEntityIdAndSuscriptionId(suscription.getId().getIdEntidadEurbt(), suscription.getId().getIdSuscripcion());
            List<String> suscriptionProducts = new ArrayList<String>();

            for (Suscripcion suscriptionProduct : suscriptionsByProduct) {
                Producto product = productRepository.findById(suscriptionProduct.getId().getIdProductoEurbt()).get();
                suscriptionProducts.add(product.getTlDescProducto());
            }

            SuscriptionDTO suscriptionDTO = mapperUtils.mapSuscriptionDataToSuscriptionDTO(suscription, suscriptionProducts);

            if (!suscriptionsDTO.contains(suscriptionDTO)) 
                suscriptionsDTO.add(suscriptionDTO);

        }

        this.entityDataDTO.add(mapperUtils.mapEntityDataToEntityDTO(entity, suscriptionsDTO));
    }

    return RepeatStatus.FINISHED;
}

@Override
public void beforeStep(StepExecution stepExecution) {

}

@Override
public ExitStatus afterStep(StepExecution stepExecution) {
    stepExecution.getJobExecution().getExecutionContext().put("entityDataDTO", this.entityDataDTO); 
    return ExitStatus.COMPLETED;
}

}

Upvotes: 0

Views: 2484

Answers (1)

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31590

Using a Tasklet to read, process and write the whole dataset is is not the best way as there will be only one transaction for the whole dataset. A chunk-oriented step is more appropriate for your use case. With chunk oriented processing, there will be one transaction for each chunk.

Hope this helps.

Upvotes: 1

Related Questions