title: How to insert data as fast as possible with Hibernate
body: <p>I read file and create a Object from it and store to postgresql database. My file has 100,000 document that I read from one file and split it and finally store to database.
  I can't create <code>List&lt;&gt;</code> and store all document in <code>List&lt;&gt;</code> because my RAM is little. My code to read and write to database are as below. But My JVM Heap fills and can not continue to store more document. How to read file and store to database efficiently.</p>
  
  <pre><code>public void readFile() {
      StringBuilder wholeDocument = new StringBuilder();
      try {
          bufferedReader = new BufferedReader(new FileReader(files));
          String line;
          int count = 0;
          while ((line = bufferedReader.readLine()) != null) {
              if (line.contains("&lt;page&gt;")) {
                  wholeDocument.append(line);
                  while ((line = bufferedReader.readLine()) != null) {
                      wholeDocument = wholeDocument.append("\n" + line);
                      if (line.contains("&lt;/page&gt;")) {
                          System.out.println(count++);
                          addBodyToDatabase(wholeDocument.toString());
  
                          wholeDocument.setLength(0);
                          break;
                      }
                  }
              }
          }
          wikiParser.commit();
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } finally {
          try {
              bufferedReader.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  
  public void addBodyToDatabase(String wholeContent) {
      Page page = new Page(new Timestamp(System.currentTimeMillis()),
              wholeContent);
      database.addPageToDatabase(page);
  }
  
  public static int counter = 1;
  
  public void addPageToDatabase(Page page) {
      session.save(page);
      if (counter % 3000 == 0) {
          commit();
      }
      counter++;
  }
  </code></pre>