Spring批处理步骤容错无法处理唯一约束

时间:2018-03-27 15:06:48

标签: java spring-batch

我正在使用spring批处理导入csv数据并将它们保存为数据库中的JPA实体。

这是我的作家:

@Bean
public ItemWriter<HotelChain> hotelChainWriter(EntityManagerFactory factory) {
    JpaItemWriter<HotelChain> writer = new JpaItemWriter<>();
    writer.setEntityManagerFactory(factory);
    return writer;
}

工作方法:

@Bean
public Step hotelStep1(ItemWriter<Hotel> writer) {
    return stepBuilderFactory.get("hotelStep1").<HotelCSVDto, Hotel>chunk(50).reader(hotelReader())
            .processor(hotelProcessor()).writer(writer)
            .faultTolerant()
            .skip(ConflictException.class)
            .skip(ConstraintViolationException.class)
            .skip(PSQLException.class)
            .noRetry(ConflictException.class)
            .noRetry(ConstraintViolationException.class)
            .noRetry(PSQLException.class)
            .skipLimit(150)
            .build();
}

错误讯息:

2018-03-27 10:26:16.596 ERROR 7493 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: duplicate key value violates unique constraint "hotel__public_id_idx"
  Detail: Key (public_id)=(HILTON_GARDEN_INN_TALLAHASSEE-US-FL-TALLAHASSEE) already exists.
2018-03-27 10:26:16.879 ERROR 7493 --- [           main] o.s.batch.core.step.AbstractStep         : Encountered an error executing step hotelStep1 in job importCityJob

org.springframework.retry.ExhaustedRetryException: Retry exhausted after last attempt in recovery path, but exception is not skippable.; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.springframework.batch.core.step.item.FaultTolerantChunkProcessor$5.recover(FaultTolerantChunkProcessor.java:405) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
    at org.springframework.retry.support.RetryTemplate.handleRetryExhausted(RetryTemplate.java:512) ...
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692) ...
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:112) ...
Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "hotel__public_id_idx"
  Detail: Key (public_id)=(HILTON_GARDEN_INN_TALLAHASSEE-US-FL-TALLAHASSEE) already exists.
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2477)...

我想知道为什么faultTolerance不起作用,我该如何解决?感谢。

0 个答案:

没有答案