title: JPA with HIBERNATE insert very slow
body: <p>I am trying to insert some data to SQL Server 2008 R2 by using JAP and HIBERNATE. Everything "works" except for that it's very slow. To insert 20000 rows, it takes about 45 seconds, while a C# script takes about less than 1 second.</p>
  
  <p>Any veteran in this domain can offer some helps? I would appreciate it a lot.</p>
  
  <p>Update: got some great advices from the answers below, but it still doesn't work as expected. Speed is the same.</p>
  
  <p>Here is the updated persistence.xml:</p>
  
  <pre><code>&lt;persistence version="2.0"
  xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
  &lt;persistence-unit name="ClusterPersist"
      transaction-type="RESOURCE_LOCAL"&gt;
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
      &lt;class&gt;cluster.data.persist.sqlserver.EventResult&lt;/class&gt;
      &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
      &lt;properties&gt;
          &lt;property name="javax.persistence.jdbc.url"
              value="jdbc:sqlserver://MYSERVER:1433;databaseName=MYTABLE" /&gt;
          &lt;property name="javax.persistence.jdbc.user" value="USER" /&gt;
          &lt;property name="javax.persistence.jdbc.password" value="PASSWORD" /&gt;
          &lt;property name="javax.persistence.jdbc.driver"
              value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /&gt;
          &lt;property name="hibernate.show_sql" value="flase" /&gt;
          &lt;property name="hibernate.hbm2ddl.auto" value="update" /&gt;
  
          &lt;property name="hibernate.connection.provider_class"
              value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" /&gt;
  
          &lt;property name="hibernate.c3p0.max_size" value="100" /&gt;
          &lt;property name="hibernate.c3p0.min_size" value="0" /&gt;
          &lt;property name="hibernate.c3p0.acquire_increment" value="1" /&gt;
          &lt;property name="hibernate.c3p0.idle_test_period" value="300" /&gt;
          &lt;property name="hibernate.c3p0.max_statements" value="0" /&gt;
          &lt;property name="hibernate.c3p0.timeout" value="100" /&gt;
          &lt;property name="hibernate.jdbc.batch_size" value="50" /&gt;
          &lt;property name="hibernate.cache.use_second_level_cache" value="false" /&gt;
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p></p>
  
  <p>And here is the updated code part:</p>
  
  <pre><code>public static void writeToDB(String filePath) throws IOException {
  
      EntityManager entityManager = entityManagerFactory.createEntityManager();
      Session session = (Session) entityManager.getDelegate();
      Transaction tx = session.beginTransaction();
      int i = 0;
  
      URL filePathUrl = null;
      try {
          filePathUrl = new URL(filePath);
      } catch (MalformedURLException e) {
          filePathUrl = (new File(filePath)).toURI().toURL();
      }
  
      String line = null;
      BufferedReader stream = null;
  
      try {
          InputStream in = filePathUrl.openStream();
          stream = new BufferedReader(new InputStreamReader(in));
  
  
          // Read each line in the file
          MyRow myRow = new MyRow();
          while ((line = stream.readLine()) != null) {
              String[] splitted = line.split(",");
              int num1 = Integer.valueOf(splitted[1]);
              float num2= Float.valueOf(splitted[6]).intValue();
  
              myRow.setNum1(num1);
              myRow.setNum2(num2);
  
              session.save(myRow);
  
              if (i % 50 == 0) { 
                  session.flush();
                  session.clear();
              }
  
              i++;
  
          }
          tx.commit();
  
      } finally {
          if (stream != null)
              stream.close();
      }
      session.close();
  
  }
  </code></pre>
  
  <p>Updated, here is the source for MyRow:</p>
  
  <pre><code>@Entity
  @Table(name="MYTABLE")
  public class MyRow {    
  
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY) 
  private Long id;
  
  @Basic
  @Column(name = "Num1")
  private int Num1;
  
  @Basic
  @Column(name = "Num2")
  private float Num2;
  
  public Long getId() {
      return id;
  }
  
  public void setId(Long id) {
      this.id = id;
  }
  
  public float getNum1() {
      return Num1;
  }
  
  public void setNum1(float num1) {
      Num1 = num1;
  }
  
  public int getNum2() {
      return Num2;
  }
  
  public void setNum2(int num2) {
      Num2 = num2;
  }
  }
  </code></pre>