2

Given: Method in a @Stateless EJB, JTA Mysql data source and a list of about 5000 entities which I'm trying to persist in a cycle :

List<MyObj> objList = entityManager.createNamedQuery("Q1", MyObj.class).getResultList();
for(MyObj obj : objList) {
  long start_time = Calendar.getInstance().getTimeInMillis();
  entityManager.persist(obj);
  long end_time = Calendar.getInstance().getTimeInMillis();
  logger.info("Object saved in " + (end_time - start_time) + "ms");
}
entityManager.close();

Log shows gradually decreasing performance from 15ms up to 180ms save time per entity. I believe Mysql sever settings are far beyond the needs of this task - it shows insignificant increase in CPU and I/O operations. Flushing EntityManager after each update has no effect.

What could be the reason for such performance decrease? Please point me in the right direction.

andbi
  • 4,426
  • 5
  • 45
  • 70

1 Answers1

3

It looks like slowdown is caused by large number of entities in persistence context (i.e. in its session cache). flush doesn't help here because it doesn't remove entities from persistence context.

If you need to process a large number of entities using a single persistence context it's recommended to clear the context with clear() periodically.

axtavt
  • 239,438
  • 41
  • 511
  • 482