Yassine EL AYACHI
Yassine EL AYACHI

Reputation: 271

Transaction Management in Spring batch

I am developing a batch using spring batch, i have two steps, a step1 that updated table1 and another step2 updated table2.

I want if the second step2 fails then all the treatment is canceled (rollback). What should I do??

I have the sample xml config below:

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

Thanks!

Upvotes: 3

Views: 12190

Answers (3)

Ramos Cemy
Ramos Cemy

Reputation: 1

Hi I hope that it can help someone else. Just need to create a step for each inspection task if these steps succeed, then launch the final step which'll save your data to database.

Upvotes: 0

Luca Basso Ricci
Luca Basso Ricci

Reputation: 18423

You can'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 automatically rollback all data stored in step1.
Maybe you can use this syntax:

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

to move next to a step used to delete data saved by step 1, but you have to know which data you have to delete in deleteDataSavedByStep1Step step.

Upvotes: 4

Related Questions