Jeyabalaji
Jeyabalaji

Reputation: 33

In spring batch, is a job (with more than one step) considered as a transaction or each step in a job is considered as a separate transaction?

I'm new to spring batch. In spring batch, is a job (with more than one step) considered as a transaction or each step in a job is considered as a separate transaction?

Thanks in advance

Upvotes: 3

Views: 1648

Answers (2)

Michael Minella
Michael Minella

Reputation: 21483

In Spring Batch, each "chunk" is executed within the scope of a transaction. In a chunk based step, each chunk is executed within the scope of a transaction. In a Tasklet step, each call to the Tasklet is wrapped within a transaction. You can read more about Spring Batch's transaction semantics in the documentation here: https://docs.spring.io/spring-batch/trunk/reference/html/transactions.html

Upvotes: 2

Har Krishan
Har Krishan

Reputation: 273

Normally you can add transaction attributes at the step level something like below:

  <job id="sampleJob" job-repository="jobRepository">
       <step id="step1">
        <tasklet transaction-manager="transactionManager">
        <chunk reader="itemReader" writer="itemWriter" commit-interval="10"/>
    </tasklet>
   </step>
 </job>

But if you want to run all the steps in single transaction then invoke the job from a transactional method something like:

        @Transactional(propagation=Propagation.REQUIRED)

In the later case it is important to consider how to handle timeouts or rollbacks.

Upvotes: 0

Related Questions