0

I am working on a Spring Batch Program, that should Read a CSV file, Process it and then send it insert its content into a Database. The Jobs are startet via rest-Joblauncher (in case that helps).

Its working most of the Time, but the SQL Connection sometimes sends an unknown "CannotCreateTransactionException" (see this post). As a workaround i want the BatchWriter to retry the writing process at least three times before it finally sends the Email.

my step Configuration :

    @Bean
public Step stepTwo() {
    StepBuilder stepBuilder = stepBuilderFactory.get("Datenimport: " + currentJobName);

    SimpleStepBuilder<TransferLsAuftrag, TransferLsAuftrag> chunk = stepBuilder
            .<TransferLsAuftrag, TransferLsAuftrag>chunk(1337);

    chunk.reader(reader());
    chunk.processor(processor());
    chunk.writer(writer());

    return chunk.build();
}

the writer implementation:

public ItemWriter<TransferLsAuftrag> writer() {
    final JdbcBatchItemWriter<TransferLsAuftrag> writer = new JdbcBatchItemWriter<TransferLsAuftrag>();
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<TransferLsAuftrag>());
    writer.setSql(
            "<long sql insert Statement>");
    writer.setDataSource(dataSource);

    return writer;
}

my custom Listener that sends (annoying) emails when the Job Fails:

    @Override
public void afterJob(JobExecution jobExecution) {
    String jobName = jobExecution.getJobInstance().getJobName();

    switch (jobExecution.getStatus()) {
    case COMPLETED:
        LOG.info(jobName + " ERFOLGREICH BEENDET");
        successfullJobs += 1;
        break;

    case STOPPED:
        // LOG.info(jobName + " wurde gestoppt");
        stoppedJobs.add(jobExecution.toString());
        break;

    case FAILED:
        failedJobs.add(jobExecution.getJobInstance().getJobName());
        Email.sendEmail("Information: " + jobName + " ist fehlgeschlagen !", jobExecution.toString());
        break;

    default:
        Email.sendEmail("Information: " + jobName + " ist fehlgeschlagen !", jobExecution.toString());
        break;
    }

I hope you guys can help me. Thank You !

eckad158
  • 385
  • 6
  • 19
  • 1
    Is the `CannotCreateTransactionException` transient? Adding retry logic only makes sense for transient exceptions where a subsequent call (after a backoff period) might succeed. If it's the case, have you tried a fault tolerant step with `FaultTolerantStepBuilder.retry(CannotCreateTransactionException.class).retryLimit(3)`? I' m asking because I see you use the `SimpleStepBuilder` and not the fault tolerant one. – Mahmoud Ben Hassine Nov 20 '19 at 08:58
  • The reoccurring Error is transient. I am now trying it with the mentioned FaultTolerantStepBuilder. I'll keep the Post updated in case it did not work as expected.Thank you once again ! – eckad158 Nov 20 '19 at 10:58
  • Changed the code like this: `SimpleStepBuilder simpleStepBuilder = stepBuilder .chunk(666); FaultTolerantStepBuilder chunk = new FaultTolerantStepBuilder<>( simpleStepBuilder); chunk.retry(CannotCreateTransactionException.class).retry(SQLServerException.class); chunk.retryLimit(3);` And the Job still failes when the Exception is thrown...i do not see any retry attempts in the catalina log – eckad158 Nov 20 '19 at 14:42

0 Answers0