users@glassfish.java.net

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

From: Piotr Nowicki <piotr_at_piotrnowicki.com>
Date: Thu, 17 Nov 2011 15:05:23 +0100

Hi Richard!

Thanks for such fast reply :-)

I've read the page you quoted, but still - if the container manages the
EntityManagerFactory and just serves you the appropriate EntityManager
while seeing

@PersistenceContext
EntityManager em

than you still don't know how does it create the EntityManagers under
the hood.

My point is that I think that it's still spec-compliant if you provide
just a single EntityManager Proxy with each time someone requires it.
Therefore, each System.out.println(em) could return the same value while
it might represent different PersistenceContexts.

If it's the Glassfish responsibility, perhaps it follow the rule of
'Each EntityManager requested by @PersistenceContext represents exactly
one PersistenceContext'. If so, it would help me a lot. If not, maybe
there is some way to check what Persistence Context is referenced by
particular EntityManager?

Thanks a lot once again.

Regards,
Piotr

> 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
>> <mailto: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
>
>
>
>