Spring批处理中的事务管理

我正在使用spring批处理开发批处理,我有两个步骤,一个更新table1的step1和另一个step2更新了table2 .

我想如果第二步2失败则所有处理都被取消(回滚) . 我该怎么办??

我有以下示例xml配置:

<b:step id="Step1" parent="Tache">
    <b:tasklet>
        <b:chunk reader="baseReader" processor="baseProcessor"
             chunk-completion-policy="completionPolicy"  />
    </b:tasklet>
</b:step>

<b:step id="Step2" parent="Tache">
    <b:tasklet>
        <b:chunk reader="baseReaderEcriture" 
            writer="ecritureWriter" chunk-completion-policy="completionPolicy"  />
    </b:tasklet>
</b:step>

<b:job id="batch" parent="Batch">
    <b:step id="step1" parent="Step1" next="step2"/>
    <b:step id="step2" parent="Step2" />
</b:job>

谢谢!

回答(2)

2 years ago

您可以't rollback already committed data (after every chunk - based on you completition policy - your data as long as spring-batch metadata are commited), so you can' t自动回滚步骤1中存储的所有数据 .
也许你可以使用这个语法:

<b:job id="batch" parent="Batch">
    <b:step id="step1" parent="Step1" next="step2"/>
    <b:step id="step2" parent="Step2">
      <next on="ROLLBACK_ALL" to="deleteDataSavedByStep1Step" />
      <end on="*" />
    </b:step>
</b:job>

要移动到用于删除步骤1中保存的数据的步骤旁边,但您必须知道在 deleteDataSavedByStep1Step 步骤中必须删除哪些数据 .