Hello,
I use OpenJPA in a distributed J2EE Environment. I implemented a Session Bean,
which lazy creates EntityManager and EntityManager Factory:
private EntityManager getEm() {
if (this.em == null) {
this.em = this.getEmf().createEntityManager();
}
return this.em;
}
private EntityManagerFactory getEmf() {
if (this.emf == null) {
this.emf =
Persistence.createEntityManagerFactory("Selektionen_DB");
}
return this.emf;
}
I persisted simple test objects with em.persist(...). Following calls with
em.find(....) failed in following constellation
- persist on clone 1
- find on clone 2
When creating EntityManager on each call (see code fragment below, without lazy
init), it works fine. But that connat be best practice, because I suppose, this
approach ist not as performant, it needs to be.
private EntityManager getEm() {
this.em = this.getEmf().createEntityManager();
return this.em;
}
private EntityManagerFactory getEmf() {
if (this.emf == null) {
this.emf =
Persistence.createEntityManagerFactory("Selektionen_DB");
}
return this.emf;
}
Which good practise do you propose?
Thank you for support
Annette Scherer
Abteilung Informatik