0

I have a Spring Batch app that, during the write step, loops through records to be inserted into a Postgres database. Every now and again we get a DuplicateKeyException in the loop, but don't want the whole job to fail. We log that record and want to continue inserting the following records.

But upon getting an exception, the transaction becomes "bad" and Postgres won't accepted any more commands, as described in this excellent post. So my question is, what's the best way to restart the transaction? Again, I'm not retrying the record that failed - I just want to continue in my loop with the next record.

This is part of my job config xml:

<batch:job id="portHoldingsJob">
    <batch:step id="holdingsStep">
        <tasklet throttle-limit="10">
             <chunk reader="PortHoldingsReader"  processor="PortHoldingsProcessor" writer="PortHoldingsWriter" commit-interval="1" />
        </tasklet>
    </batch:step>
    <batch:listeners>
        <batch:listener ref="JobExecutionListener"/>
    </batch:listeners>
</batch:job>

Thanks for any input!

CNDyson
  • 1,687
  • 7
  • 28
  • 63

1 Answers1

1

Not sure if you are using the Spring transaction annotations to manage the transactions or not ... if so perhaps you can try.

   @Transactional(noRollbackFor = DuplicateKeyException.class)

Hope that helps.

No rollback exceptions in Spring Batch are apparently designated like

<batch:tasklet>
    <batch:chunk ... />
        <batch:no-rollback-exception-classes>
        <batch:include class="MyRuntimeException"/>
    </batch:no-rollback-exception-classes>
</batch:tasklet>
James Gawron
  • 871
  • 10
  • 27