users@glassfish.java.net

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

From: Mitesh Meswani <mitesh.meswani_at_oracle.com>
Date: Thu, 17 Nov 2011 15:46:38 -0800

GlassFish uses a proxy/delegate pattern to propagate persistence
context. That is a proxy is injected into components and the actual EM
from provider is the delegate and is switched as required.

>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?
You can use EntityManager.getDelegate() to get to the actual delegate
and compare. I am curious though about why you need to compare for
persistence context equality

Regards,
Mitesh
Persistence module lead for GlassFish

On 11/17/2011 3:43 AM, 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