aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Persitence confusion. Please help. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Persitence confusion. Please help." Watch "Persitence confusion. Please help." New topic
Author

Persitence confusion. Please help.

Yucca Nel
Ranch Hand

Joined: Nov 20, 2008
Posts: 147

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?"


SCJP 6.0, SCJD (400/400), SCBCD for JEE 5, SCWCD 1.4 I do videos for development at
http://www.youtube.com/user/thejartender?feature=mhee
I am probably the only developer ever to have had an orange sized brain tumor in my brain while learning development!!
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
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 ~~

SCJP 6, SCWCD 5, SCBCD 5
 
Don't get me started about those stupid light bulbs.
 
subject: Persitence confusion. Please help.