We have a system that creates and persists a reasonably large number of objects on a daily basis (up to 50,000). When the objects are initially created, they are persisted with the following code:
Code:
EntityTransaction trans = mgr.getTransaction();
try {
trans.begin();
mgr.merge(object);
trans.commit();
}
catch (Exception e) {
LOG.error("Error persisting updated object " + object.toString() + " to database. Error: " + e.getMessage(), e);
trans.rollback();
}
(All persistence happens on a separate thread to the main application processing.)
When an object is updated, it is first cloned (a requirement of other parts of the system) and the same code as above called to persist the update to the database. The system is responsible for generating it's own Ids therefore the cloned objects have the same Id as the object being managed by the EntityManager.
When commit is called, we are seeing exceptionally high CPU usage; in fact as objects are created and updated quickly, the CPU usage frequently stays at 100%.
Connecting a profiler to the system indicates that the majority of the time is being spent in the method: org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(FlushEvent) which is called automatically by Hibernate following a commit.
I assume (but have been unable to confirm) that rather than simply writing the current change to the database, it is synchronizing all 50k managed objects which would go some way to explaining the high CPU usage.
Has anyone come across any similar issues and/or have any suggestions as to how avoid this problem?
Thanks
Steve