1

In some batch job, i call a method from a class, that's marked with:

@Transactional(propagation = Propagation.REQUIRED, noRollbackFor = {
        Fault.class,
        AnotherFault.class
})
public class ...

And, when i call this method, and an exception throws i get exception:

00:29:25,765 ERROR [org.springframework.batch.core.step.AbstractStep] (executor-2) Encountered an error executing the step: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly

To solve this problem, i try use nested transactions. Jpa don't support nested transactions, so i create dummy class with:

@Transactional(propagation = Propagation.NOT_SUPPORTED)

and call from this dummy class without transactions, my method with transactions and rollbackFor.

Result of this described here: http://postgresql.1045698.n5.nabble.com/locking-problem-in-jdbc-driver-td2174897.html

So, another way to resolve this problem - configure job:

<batch:step id="parse-step">
    <batch:tasklet>
        <batch:chunk reader="xmlCommonJob.Reader"
                     processor="xmlCommonJob.Processor"
                     writer="xmlCommonJob.Writer"
                     commit-interval="2"/>
        <batch:no-rollback-exception-classes>
            <batch:include class="com.my.common.services.fault.Fault"/>
            <batch:include class="com.my.common.services.fault.AnotherFault"/>
        </batch:no-rollback-exception-classes>
    </batch:tasklet>
    <batch:next on="FAILED" to="file-failed-step"/>
    <batch:next on="COMPLETED" to="file-success-step"/>
</batch:step>

all to no avail.

Any idea?

4
  • did you try set <tx:annotation-driven /> in the same xml where you describe the job? Commented Dec 5, 2013 at 22:27
  • It is important to indicate that in the xml with job configuration? I set <tx:annotation-driven /> in appContex.xml that's inlude batch.xml. Commented Dec 6, 2013 at 7:24
  • I had few problems time ago with tx not correctly managed and put <tx:annotation/> in job xml solve the problem; maybe this can help you, not sure Commented Dec 6, 2013 at 7:26
  • No. This is not fix for my problem. Maybe more simple to understand information: gist.github.com/IRus/cc45db58c5c15e295a12 Commented Dec 6, 2013 at 16:09

2 Answers 2

2

In Spring Batch we shouldn't catch exceptions in steps, for log in database, we should use listener:

    <batch:step id="parse-step">
        <batch:tasklet>
            <batch:chunk reader="xmlCommonJob.Reader"
                         processor="xmlCommonJob.Processor"
                         writer="xmlCommonJob.Writer"
                         commit-interval="1">
            <batch:skip-policy>
                <bean class="org.springframework.batch.core.step.skip.AlwaysSkipItemSkipPolicy" scope="step"/>
            </batch:skip-policy>
            </batch:chunk>
        </batch:tasklet>
        <batch:next on="FAILED" to="file-failed-step"/>
        <batch:next on="COMPLETED" to="file-success-step"/>
        <batch:listeners>
            <batch:listener ref="parseStepExecutionListener"/>
            <batch:listener ref="parseStepSkipListener"/>
        </batch:listeners>
    </batch:step>

in bean parseStepSkipListener i log exceptions with logger and can save info to database.

Sign up to request clarification or add additional context in comments.

Comments

1

It is not recommended to annotate business methods with @Transaction when using Spring Batch. Spring Batch handles transaction semantics within the framework so adding @Transactional can cause issues. I'd recommend removing the annotation and letting Spring Batch handle it.

1 Comment

I can remove @Transaction from my classes, but can't from business methods. I work around, and solve this problem. Soon i describe how i do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.