From my understanding
1) whether you need to invoke em.close() is depend on the transaction that you are using, that is
either "
JTA entity manager" (transactions are controlled through JTA)
or "
resource-local entity manager" (transactions are controlled by the application through the EntityTransaction API)
Container-Managed
-----------------------
When you use the code "@PersistenceContext EntityManager em", it is by default a container-managed transaction-scoped persistence context {Note: even you use @PersistenceContext(type=EXTENDED), it become container-managed extended persistence context}, which means it is JTA entity manager (because container-managed entity manager MUST be a JTA entity manager). So, the persistence context ends when the JTA transaction commits or rolls back, and all entities that were managed by the EntityManager become detached. Therefore, there is no need for you to invoke the em.close() method.
Application-Managed
------------------------
Also, when you use the code "@PersistenceUnit EntityManagerFactory emf", it will depend on the <persistence-unit> declared in your persistence.xml. If the <persistence-unit> declared without transaction-type specified, it is assumed to be JTA entity manager (you might need to use joinTransaction() to associate it with transaction if the em is created outside the scope of current JTA transaction, e.g. em created in @PostConstruct). In this case, you will be using emf.createEntityManager() to obtain an EntityManager, persistence context started here. Since it is under JTA control, the persistence context will only ends as mentioned in the paragraph above, there is no need for you to invoke the em.close() method.
For the above two cases, the persistence context still remained MANAGED until the JTA transaction ends. Calling the em.close() method only disallow you to further invoke any method of em other than getTransaction() and isOpen(), else
IllegalStateException will be thrown.
Application-Managed
------------------------
However, when you use the code "@PersistenceUnit EntityManagerFactory emf", and the transaction-type declared in your persistence.xml is RESOUCE_LOCAL, then you will be using em.getTransaction().begin() to start a transaction and em.getTransaction().commit() to end a transaction (remember that transaction is not persistence context). The persistence context will become DETACHED when you call em.close(). Therefore, in this case you need to invoke em.close() to make the entities in the persistence context to become detached.
2) No !!!
CONTAINER-MANAGED EXTENDED persistence context can ONLY be initiated within the scope of a STATEFUL session bean, which is using @PersistenceContext(type=EXTENDED).
"EntityManager obtained from EntityManagerFactory", means "@PersistentUnit EntityManagerFactory emf ; emf.createEntityManager()" which is APPLICATION-MANAGED EXTENDED persistence context, this can be use in STATELESS and STATEFUL session bean.
Hope this will help !!! I also very confuse on this concept although I have passed the exam ~~