I have the code:
public EntityManager getEntityManager() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("my-app");
EntityManager em = emf.createEntityManager();
return em;
}
for (...) {
EntityManager em = getEntityManager();
...creating myEntity
for (...) {
em.getTransaction().begin();
entityManager.persist(myEntity);
em.getTransaction().commit();
}
}
The problem: I run this code 8 AM , and I see records on database. When I run the code 9 hours later the records from 8AM are deleted
Could you please tell me if I am right:
- Even though there is always different reference to EM , there is only one persistence context.
- By default, managed entity objects that have not been modified or removed during a transaction are held in the persistence context by weak references
- During these 9 hours garbage collector removed previous entities
- EM did not "see" entites and deleteded records from DB
Is this solution correct?
add => em.clear();
for (...) {
EntityManager em = getEntityManager();
...creating myEntity
for (...) {
em.getTransaction().begin();
entityManager.persist(myEntity);
em.getTransaction().commit();
}
em.clear();
}
I found this =
http://wiki.eclipse.org/EclipseLink/FAQ/How_to_disable_the_shared_cache%3F
So, my solution is going to be OK when i add this =
<property name="eclipselink.cache.shared.default" value="false"/>