I am using JPA 2.1. Entity Managers are application managed here.
Sample Class:
public class TestDao {
private static EntityManagerFactory emf;
private static EntityManager em;
public static EntityManager initialize() {
if (emf == null) {
emf = Persistence.createEntityManagerFactory("persistence_unit");
}
return emf.createEntityManager();
}
public static void insert(Object obj){
em.persist(obj);
}
When user first time uses TestDao.initialize()
, it generates emf
and em
instances.
What will happen with this emf
instance?
Does it always keep connection with the database?
Which is better way if I have both significant reads and writes:
- create
emf
once (as I used above) - create new
emf
andem
every time I interact with the database.