lucaP
lucaP

Reputation: 11

Exception that terminate batch but without rollback

I need to insert a record into a table and then have the batch terminate with an exception without causing the insert to be rolled back.

I have used but it does not terminate the batch

Upvotes: 0

Views: 525

Answers (2)

lucaP
lucaP

Reputation: 11

I have used this:

<batch:step id="id">
        <batch:tasklet>
            <batch:chunk reader="reader" processor="processor" writer="writer" commit-interval="1">
            </batch:chunk>
            <batch:no-rollback-exception-classes>
                 <batch:include class="com.exception.myException"></batch:include>
            </batch:no-rollback-exception-classes>
        </batch:tasklet>
    </batch:step>

it allows only to avoid rollback but not terminate batch. I want terminate batch after the execution of myException.

The myException is launched in processor

Upvotes: 0

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31590

You can use a fault tolerant step and set the exceptions that should not cause a rollback using FaultTolerantStepBuilder#noRollback.

Then you can use a listener (ItemProcessListener or ItemWriteListener depending on where the exception is thrown) to intercept the exception and terminate the step (and its surrounding job) with StepExecution#setTerminateOnly.

Upvotes: 1

Related Questions