source 는 Github 에 있습니다.
// 1 cycle
// sequence 100번 호출
Hibernate:
call next value for test_sequence
// insert 쿼리 100번 호출
Hibernate:
insert
into
sequence_strategy
(name, id)
values
(?, ?)
// 위에 사이클을 100번 반복함.
@RunWith(SpringRunner.class)
@SpringBatchTest
@SpringBootTest(classes={SequenceStrategyBulkInsertJob.class, SequenceStrategyBulkInsertBean.class, TestConfig.class})
public class SequenceStrategyBulkInsertJobTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
/**
* 채번을 먼저 배치 처리해서 불러오고, 그 뒤에 insert 를 배치 처리.
* @throws Exception
*/
@Test
public void SEQUENCE_생성전략_BULK_INSERT_테스트() throws Exception {
long start = System.currentTimeMillis();
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
Assert.assertThat(jobExecution.getStatus(), is(BatchStatus.COMPLETED));
Assert.assertThat(jobExecution.getExitStatus(), is(ExitStatus.COMPLETED));
System.out.println("elapsed time : " + (System.currentTimeMillis() - start) );
}
}
@Configuration
public class SequenceStrategyBulkInsertJob {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
private final int CHUNK_SIZE = 100;
private SequenceStrategyBulkInsertBean sequenceStrategyBulkInsertBean;
public SequenceStrategyBulkInsertJob(SequenceStrategyBulkInsertBean sequenceStrategyBulkInsertBean) {
this.sequenceStrategyBulkInsertBean = sequenceStrategyBulkInsertBean;
}
@Bean
public Job sequenceStrategyBulkInsertJob01() {
return jobBuilderFactory.get("sequenceStrategyBulkInsertJob01")
.start(sequenceStrategyBulkInsertStep01())
.build()
;
}
@Bean
public Step sequenceStrategyBulkInsertStep01() {
return stepBuilderFactory.get("sequenceStrategyBulkInsertStep01")
.<SequenceStrategy, SequenceStrategy>chunk(CHUNK_SIZE)
.reader(sequenceStrategyBulkInsertBean)
.writer(sequenceStrategyBulkInsertBean)
.build();
}
}
@Slf4j
@Component
@StepScope
@RequiredArgsConstructor
public class SequenceStrategyBulkInsertBean implements ItemReader<SequenceStrategy>, ItemWriter<SequenceStrategy> {
private final SequenceStrategyRepository sequenceStrategyRepository;
// Thread not safe
private int readCount = 10000;
private int currentCount = 0;
@Override
public SequenceStrategy read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
if (currentCount == readCount) {
return null;
} else {
currentCount++;
return new SequenceStrategy("test");
}
}
@Override
public void write(List<? extends SequenceStrategy> items) throws Exception {
sequenceStrategyRepository.saveAll(items);
}
}
// 1만건의 데이터 sequence 생성방식, jdbc.batch_size : 100, chunk size : 100, h2 in memory db
elapsed time : 6696
// 1만건의 데이터 sequence 생성방식, jdbc.batch_size : X, chunk size : 100, h2 in memory db
elapsed time : 7872