Spring Package: Setting Transaction Properties at Failover Stage

I have a pretty simple step in a Spring batch job (using Java config):

@Bean
public Step step1() {
    return stepBuilders.get("stepName")
        .<Object1, Void>chunk(50)
        .reader(reader(inputResource(null))
        .processor(processor())
        .listener(stepLogger())
        .transactionAttribute(transactionTimeoutAttribute(null))
        .build();
}

   .......

@Bean
@StepScope
public StepExecutionListener stepLogger() {
    return new StepLogger();
}

@Bean
@StepScope
public TransactionAttribute transactionTimeoutAttribute(
        @Value("#{jobParameters[transactionTimeout]}") Integer timeout) {
    timeout = timeout != null ? timeout : DEFAULT_TRANSACTION_TIMEOUT;
    RuleBasedTransactionAttribute transactionAttribute = new RuleBasedTransactionAttribute();
    transactionAttribute.setTimeout(timeout);
    return transactionTimeout;
}

      

As you can see, this is a requirement that the transaction timeout can be specified as a job parameter. This works flawlessly and if I set the jobTimeout parameter too low, the job will fail until the transaction completes before the chunk completes.

However, if I try to infer fault tolerance (to skip a certain number of failed items), everything falls apart. When I add faultTolerant () to the configuration of a step to be able to specify a pass policy, etc., for example:

@Bean
public Step step1() {
    return stepBuilders.get("stepName")
        .<Object1, Void>chunk(50)
        .reader(reader(inputResource(null))
        .processor(processor())
        .faultTolerant()
        .listener(stepLogger())
        .transactionAttribute(transactionTimeoutAttribute(null))
        .build();
}

      

Spring can no longer start context (on Jetty now) and just fires the following exception on startup:

BeanCreationException: Error creating bean with name 'scopedTarget.transactionTimeoutAttribute': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope

      

What is the correct way to specify a transaction attribute and skip a step policy in Spring Package using Java Config?

EDIT: . To make the question clearer, my requirement is to make a failover step where the transaction timeout is configured as a job parameter. For a failover step, this is not a problem, just take the TransactionAttribute bean step with the job parameters connected. But FaultTolerantStepBuilder handles the transaction attribute differently (it basically merges the given transaction attribute with its internal one) so the scope of the step is not available. How can I use job parameters to configure the transaction attribute of the failover step (Java config)?

+3


source to share


1 answer


If you are using Spring package 3 or newer, you can mark the transaction attribute and step as @JobScope. This will prevent a failover step to access the transaction attribute too early.



0


source







All Articles