users@glassfish.java.net

Re: Newbie: EntityManager#persist() method & transaction

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Mon, 30 Nov 2009 18:20:24 -0800

Jean-Paul,

glassfish_at_javadesktop.org wrote:
> Hello,
>
> I have a question about the EntityManager#persist() method. I’m learning JPA
> so I already apologize if my question is trivial for you ;-)
>
> Let’s say I have 2 Classes : One Service Façade that defines the transaction
> boundary and a DAO.
>
> The first Service Façade has a business method with
> @TransactionAttribute(TransactionAttributeType.REQUIRED_NEW). This method
> calls second a method that belongs to a DAO with
> @TransactionAttribute(TransactionAttributeType.REQUIRED) attribute.

Your entity can be a DAO - you don't need to use 2 EJBs
>
> The transaction of the DAO is merged with the Service Facade transaction.
> Fine. This second method injects an EntityManager, creates an Entity that
> calls em.persist(entity). At the end of this second method, the EntityManager
> is closed and entity is detached.

If EM is injected, it's a JTA EM, and you can't close it explicitly, and the
container will close it only after transaction is completed, i.e. at the end of
the Service Façade's method.

>
> My question is: is this new detached entity (the caller could even have no
> reference of it in fact) will be committed when the Service Façade
> transaction will commit (at the end of its business method) ? Or do I have to
> explicitly merge this entity in my Service Façade in order that entity is
> committed with the Service Facade end of transaction ?

It will be committed, but it's not detached until then.

HTH,
-marina

>
> It would mean that the method where I do persist an entity is a transaction
> boundary, if not I must merge this detached entity in a caller method that is
> the transaction boundary.
>
> Help would be *very* appreciated !
>
> Thanks ! Jean-Paul DZISIAK [Message sent by forum member 'jep' ]
>
> http://forums.java.net/jive/thread.jspa?messageID=373739
>
> --------------------------------------------------------------------- To
> unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net For additional
> commands, e-mail: users-help_at_glassfish.dev.java.net
>