1

I need to scoped 14 records. chunkSize is 10 page Size is 2. It is scoping only 10 records.

I checked with different ways. chunkSize = 5 pageSize = 10 Still scoped only 10 records not all 14.

it works fine only if chunksize =11 and pageSize =10 or chunkSize = 10 and pageSize = 20

 build.gradle

  partition:
    defaultPartitionSize: 5
    partitionScopeChunkSize: 10
  jobs:
    jpaPagingSize: 2
  ===================ReaderClass============================ 
  public class PagingItemReader extends 
  JpaPagingItemReader<ScopeParams> {

     public PagingItemReader (
                              EntityManager entityManager,
                              EntityManagerFactory entityManagerFactory,
                              @Value("${spring.jobs.jpaPagingSize}") int jpaPagingSize)
        Map<String, Object> parameterValues = new HashMap<>();
        this.setQueryProvider(
                 ScopeParamsQueryProvider.buildForContinuousMatchScoping(
                      entityManager, 
                      IndustryCodes.valueFromCode(industryCd)));
        this.setEntityManagerFactory(entityManagerFactory);
        this.setPageSize(jpaPagingSize);
        this.setSaveState(true);
        this.setParameterValues(parameterValues);

  }
}

 ==============WriterClass==========
 public class JpaItemWriter<T> extends JpaItemWriter<T> {
   private JpaRepository<T, ? extends Serializable> repository;

   public JpaItemWriter(JpaRepository<T, ?> repository) {
    this.repository = repository;
    }

  @Override
  @Transactional
  public void write(List<? extends T> items) {
    persistEntities(items);
  }

  private void persistEntities(List<? extends T> list) {
    list.stream()
            .peek(item -> log.info("Writing={}", item))
            .forEach(repository::save);
  }

}

 ===================Step Configuration========
public Step WorkStep(StepBuilderFactory stepBuilderFactory,
              PagingItemReader ItemReader,
              ItemProcessor ItemProcessor,
              JpaItemWriter<Scope> itemWriter) {
    return stepBuilderFactory.get(WORK_MATCH)
            .<Scope, ExecutionScope>chunk(10)
            .reader(ItemReader)
            .processor(ItemProcessor)
            .writer(itemWriter)
            .build();
}

Processor Code,

public class MatchItemProcessor implements ItemProcessor<Scope,ExecutionScope> { 

public ExecutionScope process(Scope financialTransaction) throws Exception { 
return batchExecutionScope; 
} 
} 

private ExecutionScope prepareData(Scope transaction) { ExecutionScope executionScope = new ExecutionScope(); executionScope .setIndustryTypeCode(financialTransaction.getIndustryTypeCode()); return executionScope ; }

I am updating other object in processor with same fields on which reading happens. So i am reading "Scope" entity in reader class . In processor class creating execitionScope object and updating values based on scope and persisting execitionScope in DB.

Both entities are pointing to different tables. ScopeParam hit fin_t table and ExecutionScope hit exec_scope table.

please provide me suggestion.

user84
  • 363
  • 5
  • 16
  • most likely you are updating read entities in processor for same fields on which reading happens. Show sample query and processor code. – Sabir Khan Oct 25 '18 at 15:30
  • public class FinancialTransactionItemProcessor implements ItemProcessor { public BatchExecutionScope process(ScopeParams financialTransaction) throws Exception { BatchExecutionScope batchExecutionScope = null; if (null != financialTransaction) batchExecutionScope = prepareData(financialTransaction); } return batchExecutionScope; } } – user84 Oct 25 '18 at 15:34
  • private BatchExecutionScope prepareData(ScopeParams financialTransaction) { BatchExecutionScope batchExecutionScope = new BatchExecutionScope(); batchExecutionScope.setIndustryTypeCode(financialTransaction.getIndustryTypeCode()); return batchExecutionScope; } – user84 Oct 25 '18 at 15:39
  • I am updating other object in processor with same fields on which reading happens. So i am reading "ScopeParam" entity in reader class . In processor class creating batchExecitionScope object and updating values based on scopeParam and persisting batchExecitionScope in DB. – user84 Oct 25 '18 at 15:45
  • I have moved your comments to question. What I am asking is , do you update data in DB that is target of reading ? Even though entities are different , do they point to same table in DB ? – Sabir Khan Oct 25 '18 at 15:57
  • Thanks Sabir for replying. No, both are pointing to different tables. "ScopeParam" hit fin_t table and BatchExecutionScope hit Batch_exec_scope table. – user84 Oct 25 '18 at 16:01
  • Then , its a very strange issue. – Sabir Khan Oct 25 '18 at 16:07
  • Ok. last thing, sorry to bother you. how would i get that how many pages internally created by jpaPageItemreader ? Because if i increase chunksize 14 and pageSize =2. I am receiving all 14 records in processor class but if chuckSize =10 and pageSize=2. only 10 records prints on processor class. rest 4 records missed on reader level. – user84 Oct 25 '18 at 16:16
  • As far as I know, Spring Batch paging readers ( JDBC or JPA ) don't implement pagination that way i.e. they don't count number of pages in advance. JPA uses `ÒFFSET` & `LIMIT` clauses and it keeps track of total items read at any point in time . Total read item count is divided by page size to know current page number. So if you keep adding records to table after starting job, your job will continue running for new items added after job start too. – Sabir Khan Oct 25 '18 at 17:00
  • Thanks Sabir. It resolved. My query was not returning the records based on pagesize. i modified my query and it is working fine. :) – user84 Oct 25 '18 at 21:15
  • Can you add an answer to say it is resolved and how it was resolved? Thanks. – Mahmoud Ben Hassine Oct 26 '18 at 07:18
  • Thank you for adding the answer! – Mahmoud Ben Hassine Oct 29 '18 at 08:57

1 Answers1

0

Issue has been resolved. I got help with this link. Spring batch jpaPagingItemReader why some rows are not read?

  • Actual Issue

    JPAPagingItemReader uses offsets and limits, and if your scoping query output gets modified as part of your writer/chunking, then the next page would already have a modified data set and the offset would keep on skipping unprocessed data. Since our scoping query ignore transactions already scoped as part of any active batch, as soon as the first paged set gets chucked they fall under the omission.

  • Solution Modified my scoping query and ignore current running job.

user84
  • 363
  • 5
  • 16