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