I have the method below:
public List<Profile> listProfiles(){
EntityManager em = EMF.get().createEntityManager();
List<Profile> list = null;
try{
Query q = em.createQuery("SELECT p FROM Profile p");
list = (List<Profile>)q.getResultList();
} catch(NoResultException ex){
System.out.println("ERROR CATCHED: " + ex.getMessage());
} finally{
em.close();
}
return list;
}
Accessing the return list will throw an error:
org.datanucleus.exceptions.NucleusUserException: Object Manager has been closed
One trick I found is to add list.size() before closing entity manager:
finally{
list.size();
em.close();
}
Should I close entity manager? Or are there concepts I missed?