防止状态持久性

默认情况下,所有ItemReaderItemWriterimplementations 会存储其当前的 state 中的ExecutionContext在提交之前。但是,这可能并不总是如此 所需的行为。例如,许多开发人员选择将他们的数据库读取器 'rerunnable' 使用进程指示器。将额外的列添加到输入数据中,以 指示是否已处理。当正在读取特定记录时(或 written) 的 processed 标志从falsetrue.然后,SQL 语句可以 在where子句,例如where PROCESSED_IND = false, 从而确保在重新启动时仅返回未处理的记录。在 在这种情况下,最好不要存储任何状态,例如当前行号, 因为它在重新启动时无关紧要。因此,所有读取器和写入器都包含 'saveState' 属性。spring-doc.cadn.net.cn

以下 bean 定义显示了如何防止 Java 中的状态持久性:spring-doc.cadn.net.cn

Java 配置
@Bean
public JdbcCursorItemReader playerSummarizationSource(DataSource dataSource) {
	return new JdbcCursorItemReaderBuilder<PlayerSummary>()
				.dataSource(dataSource)
				.rowMapper(new PlayerSummaryMapper())
				.saveState(false)
				.sql("SELECT games.player_id, games.year_no, SUM(COMPLETES),"
				  + "SUM(ATTEMPTS), SUM(PASSING_YARDS), SUM(PASSING_TD),"
				  + "SUM(INTERCEPTIONS), SUM(RUSHES), SUM(RUSH_YARDS),"
				  + "SUM(RECEPTIONS), SUM(RECEPTIONS_YARDS), SUM(TOTAL_TD)"
				  + "from games, players where players.player_id ="
				  + "games.player_id group by games.player_id, games.year_no")
				.build();

}

下面的 Bean 定义显示了如何防止 XML 中的状态持久性:spring-doc.cadn.net.cn

XML 配置
<bean id="playerSummarizationSource" class="org.spr...JdbcCursorItemReader">
    <property name="dataSource" ref="dataSource" />
    <property name="rowMapper">
        <bean class="org.springframework.batch.samples.PlayerSummaryMapper" />
    </property>
    <property name="saveState" value="false" />
    <property name="sql">
        <value>
            SELECT games.player_id, games.year_no, SUM(COMPLETES),
            SUM(ATTEMPTS), SUM(PASSING_YARDS), SUM(PASSING_TD),
            SUM(INTERCEPTIONS), SUM(RUSHES), SUM(RUSH_YARDS),
            SUM(RECEPTIONS), SUM(RECEPTIONS_YARDS), SUM(TOTAL_TD)
            from games, players where players.player_id =
            games.player_id group by games.player_id, games.year_no
        </value>
    </property>
</bean>

ItemReaderconfigured 不会在ExecutionContext为 它参与的任何执行。spring-doc.cadn.net.cn