In my application I use the same session beans to create new persistent objects when objects are created in batch mode or by a client.
I have implemented something which looks like the Patch processing chapter in hibernate documentation.
All my session beans uses another SessionBean to persist objects instead of using directly the entity manager. The Session Bean in charge of persisting objects count persisted instance and flush and clear the entity manager every x objects (with x=hibernate.jdbc.batch_size)
Code:
public class PersistenceServiceBean {
@PeristenceContext
private EntityManager em;
public void persist(Object o) {
if (count%20 ==0) {
em.flush();
em.clear();
}
em.persist(o);
}
}
It makes the performance better than never flushing explicitely but I'm not very happy with that design. All my session beans that persists objects have to use my PersistenceServiceBean instead of the EntityManager. I am not able to detect if one bean uses directly the entity manager.
Is is possible to have an auto flush counter setting in the configuration (in that case I may use the entity manager directly) ?
Ideas for another design are welcome ?