Results 1 to 10 of 21

Thread: Problem with JpaPagingItemReader and pageSize

Hybrid View

  1. #1
    Join Date
    Jul 2012
    Posts
    10

    Default Problem with JpaPagingItemReader and pageSize

    We have a job configured like this :

    Batch configuration :
    Code:
        <batch:job id="minimal" job-repository="jobRepository"
            restartable="true">
    
            <batch:step id="step1">
                <batch:tasklet>
                    <batch:chunk reader="jpaPersonReader" writer="addAddressToPersonWriter" commit-interval="4" />
                </batch:tasklet>
            </batch:step>
        </batch:job>
    
        <bean id="jpaPersonReader"
            class="org.springframework.batch.item.database.JpaPagingItemReader">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
            <property name="queryString" value="select p from Person p order by p.id" />
            <property name="pageSize" value="4" />
        </bean>
        
        <bean id="addAddressToPersonWriter" class="org.jp.spring_batch_labs.AddAddressToPersonWriter" />
    AddAddressToPersonWriter class :
    Code:
    public class AddAddressToPersonWriter implements ItemWriter<Person> {
    
        private static final Logger LOG = Logger.getLogger(AddAddressToPersonWriter.class);
    
        @Override
        public void write(List<? extends Person> items) throws Exception {
            LOG.info("updating " + items.size() + " persons.");
            for (Person person : items) {
                Address address = new Address();
                address.setCp(Long.toString(System.currentTimeMillis()));
                person.addAddress(address);
            }
        }
    }
    We launch the batch with a test which initialize 15 persons in the @BeforeClass.
    15 persons are read by JpaPagingItemReader and modified by our AddAddressToPersonWriter but at the end, we can see 12 addresses in the database instead of 15.

    Our commit-interval is 4, so 12 commited modifications = 3 pages * 4 items per page, and we have the 3 last records not modified.

    Output logs :
    Code:
    --  INFO org.springframework.batch.core.launch.support.SimpleJobLauncher - Job: [FlowJob: [name=minimal]] launched with the following parameters: [{param1=1}]
    --  INFO org.springframework.batch.core.job.SimpleStepHandler - Executing step: [TaskletStep: [name=step1]]
    -- DEBUG org.springframework.batch.item.database.JpaPagingItemReader - Reading page 0
    --  INFO org.jp.spring_batch_labs.AddAddressToPersonWriter - updating 4 persons.
    -- DEBUG org.springframework.batch.item.database.JpaPagingItemReader - Reading page 1
    --  INFO org.jp.spring_batch_labs.AddAddressToPersonWriter - updating 4 persons.
    -- DEBUG org.springframework.batch.item.database.JpaPagingItemReader - Reading page 2
    --  INFO org.jp.spring_batch_labs.AddAddressToPersonWriter - updating 4 persons.
    -- DEBUG org.springframework.batch.item.database.JpaPagingItemReader - Reading page 3
    --  INFO org.jp.spring_batch_labs.AddAddressToPersonWriter - updating 3 persons.
    --  INFO org.springframework.batch.core.launch.support.SimpleJobLauncher - Job: [FlowJob: [name=minimal]] completed with the following parameters: [{param1=1}] and the following status: [COMPLETED]
    --  INFO org.jp.spring_batch_labs.SimpleTestJpa - 12 adresses in the db

    We noticed that the JpaPagingItemReader has been modified since version 1.1.3 : the flush has been moved from the end of the method doReadPage to the beginning of this method. If we write our own JpaPagingItemReader with a flush at the end of the doReadPage method, we solve our problem.

    So :
    - why was the flush moved from the end to the beginning of the method ?
    - is there any risk in replacing it at the end ?
    - is there a Jira that references this pb and will it be soon fixed ?

    Thank you for your answers,

    JP

  2. #2

    Default

    Just curious... Could you run your test with a commit-interval="1" to see if the results differ?

    Thanks,

    Jeff

  3. #3

    Default

    If you do change the commit-interval for a test... It nees to matche the page size used by JpaPagingItemReader.

  4. #4

    Default

    Wow, did I have a brain fart.

    It nees to matche the page size used by JpaPagingItemReader.

    Should be:

    It needs to match the page size used by JpaPagingItemReader.

    Anyway, I suspect you already know this.

    Jeff

  5. #5
    Join Date
    Jul 2012
    Posts
    10

    Default

    The behaviour is the same : the items of the last page are not commited ==> with pageSize = 1, the last record will not have is modifications persisted.

  6. #6

    Default

    Commit interval is related to writer, and in the log you provided it's receiving the 15 records.
    Code:
    -- DEBUG org.springframework.batch.item.database.JpaPagingItemReader - Reading page 0
    --  INFO org.jp.spring_batch_labs.AddAddressToPersonWriter - updating 4 persons.
    -- DEBUG org.springframework.batch.item.database.JpaPagingItemReader - Reading page 1
    --  INFO org.jp.spring_batch_labs.AddAddressToPersonWriter - updating 4 persons.
    -- DEBUG org.springframework.batch.item.database.JpaPagingItemReader - Reading page 2
    --  INFO org.jp.spring_batch_labs.AddAddressToPersonWriter - updating 4 persons.
    -- DEBUG org.springframework.batch.item.database.JpaPagingItemReader - Reading page 3
    --  INFO org.jp.spring_batch_labs.AddAddressToPersonWriter - updating 3 persons.
    ItemReader is not meant to update any data, just retrieve them, AFAIK.
    And why not using JdbcPagingItemReader? Better performance.

    visualjeff you could just edit your post instead of flooding.
    Last edited by traduz; Jul 26th, 2012 at 09:30 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •