Preventing State Persistence

默认情况下,所有 ItemReaderItemWriter 实现都会在提交前将它们当前的状态存储在 ExecutionContext 中。但有时这可能并非期望的行为。例如,许多开发人员会选择通过使用进程指标来使他们的数据库读取器“可重新运行”。在输入数据中会添加一个额外列,以指示数据是否已处理。当正在读取(或写入)特定记录时,处理标志将从 false 翻转为 true。然后,SQL 语句就可以在 where 子句中包含一个额外的语句,如 where PROCESSED_IND = false,从而确保在重新启动的情况下只返回未处理的记录。在这种情况下,最好不存储任何状态(如当前行号),因为它在重新启动时无关紧要。出于这个原因,所有读取器和写入器都包含“saveState”属性。

By default, all of the ItemReader and ItemWriter implementations store their current state in the ExecutionContext before it is committed. However, this may not always be the desired behavior. For example, many developers choose to make their database readers 'rerunnable' by using a process indicator. An extra column is added to the input data to indicate whether or not it has been processed. When a particular record is being read (or written) the processed flag is flipped from false to true. The SQL statement can then contain an extra statement in the where clause, such as where PROCESSED_IND = false, thereby ensuring that only unprocessed records are returned in the case of a restart. In this scenario, it is preferable to not store any state, such as the current row number, since it is irrelevant upon restart. For this reason, all readers and writers include the 'saveState' property.

Java

以下 Bean 定义展示了如何在 Java 中防止状态持久性:

The following bean definition shows how to prevent state persistence in Java:

Java Configuration
@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();

}
XML

以下 Bean 定义展示了如何在 XML 中防止状态持久性:

The following bean definition shows how to prevent state persistence in XML:

XML Configuration
<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>

上面配置的 ItemReader 不会对其参与的任何执行在 ExecutionContext 中创建任何条目。

The ItemReader configured above does not make any entries in the ExecutionContext for any executions in which it participates.