1

I have a Spring batch step executing a tasklet that polls for files on a remote server:

public class MyConfiguration extends AbstractConfiguration {
   
    @Bean
    @Qualifier("pollStep")
    public Step pollStep() {
        return stepBuilderFactory.get("pollStep")
                                 .tasklet(filePollingTasklet())
                                 .listener(promoteContextListener())
                                 .build();
    }

    @Bean
    @StepScope
    private Tasklet filePollingTasklet() {
        return ((stepContribution, chunkContext) -> getStatus(stepContribution, chunkContext));
    }

    private RepeatStatus getStatus(StepContribution stepContribution, ChunkContext chunkContext) {
        //some code
        Map<String, Boolean> result = poller.pollForFile(myContext, sourceInfo);
        return RepeatStatus.FINISHED;
    }

}

It polls for up to 100 minutes, retrying every so often...

When it is out of while loop polling for file in pollForFile() method, it throws a RuntimeException if it cannot find the file.

the spring batch STEP EXECUTION table records the EXIT MESSAGE for this step with exception as follows when the batch fails in this instance:

org.springframework.transaction.TransactionSystemException: Could not roll back JPA transaction; nested exception is org.hibernate.TransactionException: Unable to rollback against JDBC Connectionat org.springframework.orm.jpa.JpaTransactionManager.doRollback(JpaTransactionManager.java:565)at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:835)at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:809)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)at com.sun.proxy.$Proxy177.rollback(Unknown Source)at org.springframework.transaction.support.TransactionTemplate.rollbackOnException(TransactionTemplate.java:168)at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:144)at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:273)at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:82)at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375)at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145)at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:258)at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:208)at org.spring

I would like to understand why the exit message is a jpa rollback error and not the runtime exception? How can I ensure that in this case the runtime exception message is what gets stored in Exit Message in spring batch table when the step fails.

M06H
  • 1,675
  • 3
  • 36
  • 76

1 Answers1

0

I would like to understand why the exit message is a jpa rollback error and not the runtime exception?

Because this is what actually makes your step fails. The stack trace you shared is truncated, but the runtime exception should be the cause of org.springframework.transaction.TransactionSystemException, which in turn is the cause of your step failure.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • Thanks, how to avoid this error? Have I misconfigured my step somewhere ? – M06H May 11 '21 at 08:37
  • You can't avoid this error (if the transaction fails then the step will fail as well), unless you catch runtime exceptions around your code and decide what to do with them. – Mahmoud Ben Hassine May 11 '21 at 08:49
  • it seems the connection to my sqlserver database is being reset or times out due to long running tasklet. in this case if I increase the timeout interval for sql server reset, will this suffice or is there a setting to keep the connections alive ? – M06H May 11 '21 at 14:43
  • 1
    This is another question and not related to the initial one. I see you have asked it here: https://stackoverflow.com/questions/67491044/spring-batch-long-running-tasklet-database-timeout. Please accept this answer here as I believe it answers your initial question and I will try to help on the other one. – Mahmoud Ben Hassine May 12 '21 at 10:10
  • appreciate if you can help to resolve the other one which I've been stuck with for some time – M06H May 12 '21 at 14:26