1

I have a jpa code that genrates players (10000) and for each player 100 game and inserts them in a db but it's so slow i've tried using btch writing where i've set the btch size and then used the flush / clean methods but that hasn't changed much


  List<Player> players = new ArrayList<>();
  Random rd = new Random();

  @Override
  public void createMassData() {
    EntityManager em = lab02EntityManager.getEntityManager();
    EntityTransaction tx = em.getTransaction();
    List<Category> allCategories =
        lab02EntityManager
            .getEntityManager()
            .createQuery("select c from Category c")
            .getResultList();
    players = createPlayers();
    tx.begin();
    int trans = 1000;
    int index=0;
    for (Player player : players) {
      em.persist(player);
      index++;
      for (int i = 0; i < 10; i++) {
        em.persist(playGame(player, allCategories));
        index++; }
        if (index > trans){
          index=0;
          em.flush();
          em.clear();
        }
      }
     System.out.println("created ..");
    tx.commit();
  }


  • You *may* want to review: https://stackoverflow.com/questions/10584179/batch-inserts-using-jpa-entitymanager – PM 77-1 Jan 15 '20 at 18:44
  • You can try (see https://vladmihalcea.com/the-best-way-to-do-batch-processing-with-jpa-and-hibernate/) to also commit (and begin again) the transaction – Beppe C Jan 15 '20 at 18:58
  • One thing I would definitely try is to have 2 independent loops (one for creating all players first, the other to store the playGame data), both can use the bulk insert approach you have already identified – Beppe C Jan 15 '20 at 19:00

0 Answers0