• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Persitence confusion. Please help.

Ranch Hand
Posts: 147
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friendly ranchers! I am having 2 issues bothering me here.

All of these question or on what I have read in EJB 3 from oreilly and I need some help.
1)We may never call .close on an injected EMFactory or EManager. Does this mean that an EManger obtained from a entityMangerFactory may not be closesd if the EMFactory was injected?
2)It states that an EntityManager obtained from EMFactory is by default EXTENDED type so does that mean that we can not use EMFactory in stateless session beans as "You may only use EXTENDED in stateful session bean?"
Ranch Hand
Posts: 213
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)

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.

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.

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 ~~
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic