users@glassfish.java.net

Re: How does Glassfish manage Persistence Contexts (equality test)?

From: Richard Kolb <rjdkolb_at_openrap.org>
Date: Thu, 17 Nov 2011 14:01:07 +0200

Hi Piotr

I won't pretend to understand the inner working of JPA inside the
container, but I did see this interesting doc from TomEE that may help :
http://openejb.apache.org/jpa-concepts.html

*Calling entityManagerFactory.createEntityManager() twice results in two
separate EntityManager instances and therefor two separate
PersistenceContexts/Caches. -- It is almost never a good idea to have more
than one instance of an EntityManager in use (don't create a second one
unless you've destroyed the first)*

-----
When I do the below and the EJB is invoked all over the place, do the
EntityManager's get pooled since I did not use the factory, is there only
one ? I am not sure.

@Stateless
public class PartyFacadeCRUDBean {
    @PersistenceContext(unitName = "BLA")
    private EntityManager em;

}


Hope this helps
regards
Richard.



On 17 November 2011 13:43, <piotr_at_piotrnowicki.com> wrote:

> Howdy Guys,
>
> I've been asking this question for some time now (StackOverflow,
> EclipseLink, JavaRanch) - without any luck.
>
> If I correctly understand the JPA 2.0 specification, it's the container
> (Glassfish) responsible for processing the persistence.xml, discovering the
> JPA Provider and creating an EntityManagerFactory. If so, than it's *the
> container responsibility* to manage the EntityManagers? In the
> specification, there is the following part:
>
> "When operating with a third-party persistence provider, the container
> uses the contracts defined in section 7.9 to create and destroy
> container-managed persistence contexts. *It is undefined whether a new
> entity manager instance is created for every persistence context, or
> whether entity manager instances are sometimes reused*. Exactly how the
> container maintains the association between persistence context and JTA
> transaction is not defined."
>
> As I understand it, it allows the container (or the jpa-provider?) to
> either create new EntityManager each time or reuse it's instances (perhaps
> using a proxy?).
>
> If so, than how is Glassfish managing the EntityManagers?
> Is there a way I can check two EntityManager instances for equality?
> Can I do naive System.out.println(em) and check the value for
> PersistenceContexts equality?
>
> Thanks in advance!
>
> Regards,
> Piotr
>