title: how to increase the performance of bulk inserts using session factory
body: <p>I am using jpa with hibernate using seam. Ineed to insert 200,000 records at a time.</p>
  
  <p>Here is my code: </p>
  
  <p>hibernate.cfg.xml</p>
  
  <pre><code>&lt;hibernate-configuration&gt;
   &lt;session-factory name="java:/mobeeSessionFactory"&gt;
    &lt;property name="hibernate.connection.pool_size"&gt;10&lt;/property&gt;
    &lt;property name="hibernate.connection.driver_class"&gt;oracle.jdbc.OracleDriver&lt;/property&gt;
    &lt;property name="hibernate.connection.url"&gt;jdbc:oracle:thin:@localhost:1521:mobee&lt;/property&gt;
    &lt;property name="hibernate.connection.username"&gt;mobeemigrate&lt;/property&gt;
    &lt;property name="hibernate.connection.password"&gt;mobeemigrate&lt;/property&gt;
    &lt;property name="hibernate.show_sql"&gt;false&lt;/property&gt;
    &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.OracleDialect&lt;/property&gt;
    &lt;property name="hibernate.format_sql"&gt;false&lt;/property&gt;
    &lt;property name="hibernate.hbm2ddl.auto"&gt;update&lt;/property&gt;
    &lt;property name="hibernate.session_factory_name"&gt;java:/mobeeSessionFactory&lt;/property&gt;
    &lt;property name="hibernate.connection.datasource"&gt;mobeeadminDataSource&lt;/property&gt;
    &lt;property name="hibernate.jdbc.batch_size"&gt;10000&lt;/property&gt;
    &lt;property name="hibernate.cache.use_first_level_cache"&gt;true&lt;/property&gt;
    &lt;property name="hibernate.cache.use_second_level_cache"&gt;false&lt;/property&gt;
    &lt;property name="hibernate.transaction.manager_lookup_class"&gt;org.hibernate.transaction.JBossTransactionManagerLookup&lt;/property&gt;
    &lt;property name="hibernate.transaction.factory_class"&gt;org.hibernate.transaction.JTATransactionFactory&lt;/property&gt;
    &lt;property name="hibernate.transaction.auto_close_session"&gt;false&lt;/property&gt;
    &lt;property name="hibernate.cache.provider_class"&gt;org.hibernate.cache.HashtableCacheProvider&lt;/property&gt;
    &lt;property name="hibernate.transaction.flush_before_completion"&gt;true&lt;/property&gt;
  
    &lt;!-- Here are the mappings --&gt;
    &lt;mapping package="com.manam.mobee.persist.entity"/&gt;
    &lt;mapping class="com.manam.mobee.persist.entity.TempCustomers"/&gt;
    &lt;mapping class="com.manam.mobee.persist.entity.TempAccounts"/&gt;
   &lt;/session-factory&gt;
  </code></pre>
  
  <p>component.xml</p>
  
  <pre><code>&lt;persistence:hibernate-session-factory name="hibernateSessionFactory" cfg-resource-name="hibernate.cfg.xml"/&gt;
      &lt;persistence:managed-hibernate-session name="session" 
                   auto-create="true"
       session-factory-jndi-name="java:/mobeeSessionFactory"/&gt;
  </code></pre>
  
  <p>Sample Code:</p>
  
  <pre><code> Session session =hibernateSessionFactory.openSession();
               Transaction tx = session.beginTransaction();
  
     for(int i=0;i&lt;doTempAccounts.size();i++){
  
       try {
      TempAccounts temp=new TempAccounts();
  
      BeanUtils.copyProperties(temp, doTempAccounts.get(i));
  
      session.save(temp);
  
      if ( i % 10000 == 0 ) { //10000, same as the JDBC batch size
       //flush a batch of inserts and release memory:
      log.info("********** Batch Updates**********");
      session.flush();
      session.clear();
       }
       }
       }
      tx.commit();
      session.close();
  </code></pre>
  
  <p>The above code is working but it takes around 10 minutes to insert 200,000 records. Is there any configuration in my code to increase the performance of bulk inserts?</p>
  
  <p>In above code I do a session.flush() every 10000 records but its not insert Database every time.  Can you explain how to perform bulk inserts to db.?</p>