Spring批量分页问题

时间:2016-06-30 15:23:23

标签: spring spring-batch batch-processing

我正在使用Spring Batch,我正在使用分页来从DB中获取数据。它第一次触发查询按预期,但对于第二页,它添加了一个更多的意外参数。我不明白为什么。

我在XML文件中的查询是

<bean id="summaryAppReader" scope="step" autowire-candidate="false"
    class="org.springframework.batch.item.database.JdbcPagingItemReader">
    <property name="dataSource" ref="dataSource" />
    <property name="rowMapper">
        <bean class="com.majesco.nyl.batch.mapper.SummaryApplicationRowMapper" />
    </property>
    <property name="queryProvider">
        <bean
            class="org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="fromClause" value="VIEW_SUMMARY_APPLICATION" />
            <property name="selectClause"
                value="application_id, upper(application_status)  as application_status,relationship_code, create_date,submission_date,transmission_date,TRANSMISSION_STATUS,tpa_notes,site_details_id,transaction_num,PROCESS_STATUS,process_date,application_status_date,first_name,middle_name,last_name,suffix,prefix,APPROVED_REJECTED_DATE,action_by,ass_acronym,user_name,applicant_name,site_name,ASSOCIATION_ID, (TO_DATE('#{jobParameters['runDate']}', 'yyyy-mm-dd') - trunc(create_date)) as elapsedDays" />
            <property name="sortKeys">
                <map>
                    <entry key="transaction_num" value="ASCENDING" />
                </map>
            </property>
            <property name="whereClause"
                value="(upper(application_status)='PENDING' OR (upper(application_status) IN ('APPROVED','DECLINED') AND trunc(application_status_date) = TO_DATE('#{jobParameters['runDate']}', 'yyyy-mm-dd'))) and TRANSACTION_NUM &gt;= :minId and TRANSACTION_NUM &lt;= :maxId" />
        </bean>
    </property>
    <property name="parameterValues">
        <map>
            <entry key="minId" value="#{stepExecutionContext[minValue]}" />
            <entry key="maxId" value="#{stepExecutionContext[maxValue]}" />
        </map>
    </property>
</bean>

用于获取第一页的数据,其触发查询类似于此及其预期的

Reading page 0
SQL used for reading first page: [SELECT * FROM (SELECT application_id, upper(application_status)  as application_status,relationship_code, create_date,submission_date,transmission_date,TRANSMISSION_STATUS,tpa_notes,site_details_id,transaction_num,PROCESS_STATUS,process_date,application_status_date,first_name,middle_name,last_name,suffix,prefix,APPROVED_REJECTED_DATE,action_by,ass_acronym,user_name,applicant_name,site_name,ASSOCIATION_ID, (TO_DATE('2016-06-29', 'yyyy-mm-dd') - trunc(create_date)) as elapsedDays FROM VIEW_SUMMARY_APPLICATION WHERE (upper(application_status)='PENDING' OR (upper(application_status) IN ('APPROVED','DECLINED') AND trunc(application_status_date) = TO_DATE('2016-06-29', 'yyyy-mm-dd'))) and TRANSACTION_NUM >= :minId and TRANSACTION_NUM <= :maxId ORDER BY transaction_num ASC) WHERE ROWNUM <= 10]
Using parameterMap:{minId=100000002630, maxId=100000002663}

但是对于第二页,它在Query _TRANSACTION_NUM中添加了一个更意外的参数,如

Reading page 1
SQL used for reading remaining pages: [SELECT * FROM (SELECT application_id, upper(appication_status)  as application_status,relationship_code, create_date,submission_date,transmission_date,TRANSMISSION_STATUS,tpa_notes,site_details_id,transaction_num,PROCESS_STATUS,process_date,application_status_date,first_name,middle_name,last_name,suffix,prefix,APPROVED_REJECTED_DATE,action_by,ass_acronym,user_name,applicant_name,site_name,ASSOCIATION_ID, (TO_DATE('2016-06-29', 'yyyy-mm-dd') - trunc(create_date)) as elapsedDays FROM VIEW_SUMMARY_APPLICATION WHERE (upper(application_status)='PENDING' OR (upper(application_status) IN ('APPROVED','DECLINED') AND trunc(application_status_date) = TO_DATE('2016-06-29', 'yyyy-mm-dd'))) and TRANSACTION_NUM >= :minId and TRANSACTION_NUM <= :maxId ORDER BY transaction_num ASC) WHERE ROWNUM <= 10 AND ((transaction_num > :_transaction_num))]
Using parameterMap:{minId=100000002596, maxId=100000002629, _transaction_num=100000002622}

FYI。 TRANSACTION_NUM不是唯一的。

1 个答案:

答案 0 :(得分:0)

这是由于你的if exists (select id from myitemtable where onhand < 200) begin << send email routine >> end 。您定义了以下内容:

sortKeys

Spring Batch使用第0页中的最高 <property name="sortKeys"> <map> <entry key="transaction_num" value="ASCENDING" /> </map> </property> ,并说下一页的transaction_num必须高于transaction_num

框架将为每个页面增加绑定变量,以确保您获得独特的结果。这里的问题是你实际上需要一个主键才能正确分页,否则你的pageSize(默认值= 10,并且看到它在你的查询中添加WHERE ROWNUM <= 10的地方)会切断你的一些记录。