私はDBに多くのテストデータを入力しようとしていますので、CommandLineRunnerを書いて約2k個のエンティティを保存しました。spring boot jpa - テストデータを生成して保存する
それは仕事ですが、終わりまでには(5-10分)かかります - これは間違ったやり方ですか?
@Component
public class DbSeederTest implements CommandLineRunner {
@Autowired
FirstRepo firstRepo;
@Autowired
SecondRepo secondRepo;
@Autowired
ThirdRepo thirdRepo;
private List<FirstEnt> firstList = new ArrayList<>();
private List<SecondEnt> secondList = new ArrayList<>();
private List<ThirdEnt> thirdList = new ArrayList<>();
private void generateTestData() {
// generate alot of entities, and add them to the Lists
}
@Override
public void run(String... args) throws Exception {
System.out.println("saving ents...");
generateTestData();
try {
firstRepo.save(firstList);
secondRepo.save(secondList);
thirdRepo.save(thirdList);
} catch(Exception e) {
e.printStackTrace();
}
}
}
私はなぜその時間がかかりすぎるのか分かりませんが、スレッドコンセプトを使用することで確実に時間を短縮できます。 'generateTestData();'の後にデータがすでに設定されているので、今度は別のスレッドですべてのrepo saveメソッドを呼び出します。 – SachinSarawgi