This test from the Memory Leak thread uses a separate transaction, and moreover, a new EntityManager per stored entity object. A batch load should reuse the EntityManager and persist a large number of objects per transaction. This is explained in the Storing JPA Entity Objects page in the manual.
Attached a modified version of your TestObjectDB.java file that commits once per 10,000 entity objects. On Intel Core 2 Quad Q6600 and enhancement by Java Agent, this version persists 7,000 Device instances per second.
But Device is a large object with an array of 100 strings (by the way, List<String> is preferred over String[] when using JPA). With 3 strings the rate is about 80,000 Device instances per second, and with null in the String[] array the rate goes up to 200,000 entity objects per second:
[THREAD-0] Wed May 04 01:25:45 IDT 2011 Persisted 980000 objects.
[THREAD-0] Wed May 04 01:25:45 IDT 2011 Persisted 990000 objects.
[THREAD-0] Wed May 04 01:25:45 IDT 2011 Persisted 1000000 objects.
[THREAD-0] Wed May 04 01:25:45 IDT 2011 Added 1000000 objects, elapsed time: 4985ms
Here is a simple program that tests batch load of an entity with only a primary key:
public final class InsertTest
{
public static void main(String[] args)
{
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("$objectdb/db/speed.odb");
EntityManager em = emf.createEntityManager();
int count = 1000000;
long startTime = System.currentTimeMillis();
em.getTransaction().begin();
for (int i = 1; i <= count; i++)
{
em.persist(new MyEntity(i));
if ((i % 10000) == 0)
{
em.getTransaction().commit();
em.getTransaction().begin();
}
}
em.getTransaction().commit();
long time = System.currentTimeMillis() - startTime;
long rate = count / time * 1000;
System.out.println("Persisted " + rate + " objects per second.");
em.close();
emf.close();
}
@Entity
static final class MyEntity
{
@Id int id;
MyEntity(int id) {
this.id = id;
}
}
}
On Intel Core 2 Quad Q6600 (and enhancement) it persists 340,000 objects per second:
Persisted 340000 objects per second.
Finally, here is the batch insert speed comparison in the JPA benchmark:
https://jpab.org/Basic/Persist/Many.html