title: JPA: How to commit?
body: <p>I am using JPA to insert a lot of objects into the database (75.000), knowing that the code below can be called multiple times...</p>
  
  <p>Since I do not want a memory leak, I am asking JPA to flush the <code>EntityManager</code> after 5.000 <code>persist()</code>. Indeed, flushing is not commiting anything...</p>
  
  <pre><code>@Stateless
  public class TwitterScheduler {    
  
      @EJB
      private EntityUserFacade facade;
  
      // ids are 5.000
      public void do(ids) {
          for (long id : ids.getIDs()) {
              Entity e = new Entity();
              e.set(...);
              this.facade.create(e);
          }
          this.facade.getEntityManager().flush();
      }
  }
  </code></pre>
  
  <ol>
  <li>If I annote my class with <code>@TransactionManagement(TransactionManagementType.BEAN)</code>, <code>persist()</code> will automaticly <code>commit()</code> the transaction if I am right.</li>
  </ol>
  
  <p>But I am <strong>worried about performance / memory leak</strong>, should I ?</p>
  
  <ol start="2">
  <li><p>Should I use <code>@Resource UserTransaction utx;</code> to join somehow the transaction to JPA ? If so how ? Because I cannot is the Transaction Management is set to <code>Container</code>.</p></li>
  <li><p>Also I wonder, if I DO NOT FLUSH the EntityManager, I got a memory leak. If I flush the EntityManager using <code>this.facade.getEntityManager().flush();</code>, it became very slow the more I called <code>do()</code> method, why ?</p></li>
  </ol>