users@glassfish.java.net

Re: How does Glassfish manage Persistence Contexts (equality tes

From: <forums_at_java.net>
Date: Thu, 17 Nov 2011 17:53:37 -0600 (CST)

Hi Guys,
 
It's the third time I'm trying to post this message - once it was attached to
different topic, and the second one just vanished...
 
Nevertheless, I will repost the previously sent message and after, I'll add
some explanation.
 
@Jason - You're right, I didn't precise it: I'm interested in
container-managed transaction-scoped EntityManagers only.
 
Here goes the 'old' reply message:
-------------------------------------
 
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?
 
-------------------------------------
 
Ok, and now some explanation “why do I bother with this whole thing”. The
point is, that recently I've bumped into a user which wanted to test what
EntityManager he is referencing to or maybe more precisely - if the invoked
method is in the same transaction (uses the same PersistenceContext) as the
invoker.
 
Therefore, there was a code like this:
 
@Stateless
public class MyEJB {
   
    @PersistenceContext
    EntityManager em;
 
    public void method1() {
        System.out.println(em);
        method2();
    }
 
    @TransactionAttribute(REQUIRES_NEW)
    public void method2() {
        System.out.println(em);
    }
}
 
The client invokes the EJB's method1(). As you surely noticed, the method2()
call will be made locally - not through the EJB, so the @TransactionAttribute
doesn't have any meaning. There is no
SessionContext#getBusinessObject(-).method2(), so the method2() will share
the same transaction with method1().
 
The point that interested me is that it's not the first time I see people
comparing the output of EntityManager#toString() method and, if they match,
they treat it as a proof that the PersistenceContexts are the same.
 
That's why I'm asking - can such comparison be really meaningful or because
of proxy'ing it's worthless?
 
Thanks in advance for your time Guys :-)
 
Regards.


--
[Message sent by forum member 'PedroKowalski']
View Post: http://forums.java.net/node/865038