users@glassfish.java.net

Re: EntityManager, EntityManagerFactory and UserTransaction

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Mon, 27 Jul 2009 09:42:04 -0700

Hello,

glassfish_at_javadesktop.org wrote:
> Hello,
>
> I'm having this servlet:
>
> public class GreetingServiceImpl {
>
> @Resource UserTransaction userTransaction;
> @PersistenceUnit(unitName="test") EntityManagerFactory emf; public
> GreetingServiceImpl(){ //emf =
> Persistence.createEntityManagerFactory("test");
>
> ......
>
>
>
> 1. I'm wondering what's the difference between having the
> EntityManagerFactory injected with the help of the annotation and creating it
> throught the call Persistence.createEntityManagerFactory("test") in the
> constructor.

There is a big difference - EMF created via Persistence.createEMF can't be JTA
and its EMs can't participate in a container transaction, which also means that
if your transaction propagates from one EJB to another, your persistence context
doesn't.

>
>
> 2. Also, further in the code, what would be the difference between using the
> injected UserTransaction and the EntityManager's getTransaction() method.

UserTransaction starts a JTA transaction, EntityTransaction doesn't. Think of
the latter as a commit operation on a connection.

>
> 3. When using @PersistenceContext directly (either on the class or directly
> on some servlet member - see
> http://weblogs.java.net/blog/ss141213/archive/2005/12/dont_use_persis_1.html)
> is there a way to get the hand on its EntityManagerFactory?

Not in 1.0.

Regards,
-marina

>
> Thanks. [Message sent by forum member 'vladbalan' (vladbalan)]
>
> http://forums.java.net/jive/thread.jspa?messageID=357606
>
> --------------------------------------------------------------------- To
> unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net For additional
> commands, e-mail: users-help_at_glassfish.dev.java.net
>