我尝试从csv文件导入城市数据时,某些数据可能会重复,从而调用冲突错误ERROR: duplicate key value violates unique constraint "city__unique_idx"
Detail: Key (country, city_name, latitude, longitude)=(231, Monticello, 30.5450000000000017, -83.8703000000000003) already exists.
2018-03-13 14:34:03.607 ERROR 13275 --- [ main] o.s.batch.core.step.AbstractStep : Encountered an error executing step cityStep1 in job importCityJob
。我想知道如何忽略此错误并保持作业运行,因为它当前立即退出。
编剧:
@Bean
public ItemWriter<City> cityWriter(EntityManagerFactory factory) {
JpaItemWriter<City> writer = new JpaItemWriter<>();
writer.setEntityManagerFactory(factory);
return writer;
}
这是我的工作方法:
@Bean
public Step cityStep1(ItemWriter<City> writer) {
return stepBuilderFactory.get("cityStep1").<City, City>chunk(10).reader(cityReader())
.processor(cityProcessor()).writer(writer).build();
}
@Bean
public Job importHotelJob(JobCompletionNotificationListener listener, Step cityStep) {
return jobBuilderFactory.get("importCityJob").incrementer(new RunIdIncrementer())
.listener(listener)
.next(cityStep1)
.build();
感谢。
EDIT1:
我申请faultTolerant()
后
@Bean
public Step cityStep1(ItemWriter<City> writer) {
return stepBuilderFactory.get("cityStep1").<City, City>chunk(50).reader(cityReader())
.processor(cityProcessor()).writer(writer)
.faultTolerant()
.skip(ConflictException.class)
.skip(ConstraintViolationException.class)
.noRetry(ConflictException.class)
.noRetry(ConstraintViolationException.class)
.skipLimit(150)
.build();
我仍然收到错误:
2018-03-14 15:49:40.047 ERROR 26613 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: duplicate key value violates unique constraint "city__unique_idx"
Detail: Key (country, city_name, latitude, longitude)=(231, Monticello, 30.5450000000000017, -83.8703000000000003) already exists.
2018-03-14 15:49:40.161 ERROR 26613 --- [ main] o.s.batch.core.step.AbstractStep : Encountered an error executing step cityStep1 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) ~[spring-retry-1.2.1.RELEASE.jar:na]
at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:351) ~[spring-retry-1.2.1.RELEASE.jar:na]
at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:211) ~[spring-retry-1.2.1.RELEASE.jar:na]
答案 0 :(得分:0)
您需要使步骤容错特定异常。
@Bean
public Step cityStep1(ItemWriter<City> writer) {
return stepBuilderFactory.get("cityStep1").<City,City>chunk(10).reader(cityReader())
.processor(cityProcessor()).writer(writer).faultTolerant()
.skip(YourException.class).noRetry(YourException.class)
.noRollback(YourException.class).skipLimit(10).
build();
}
您没有列出您收到的作家或例外,因此您可以将该例外替换为YourException
。
noRetry
,noRollBack
已被用来改变默认的Spring Batch行为以防跳过弹簧批回滚事务的整个块和&amp;然后重新处理项目bu项目。如果您对此方式没问题,可以从步骤定义中删除这些部分。