> If you like to use UserTransactions, then check our JPA module. Guess Gerhard 
>added a sample for it.
Sorry I've looked but didn't found it ;(

> On a few servers the EE - injected EMF will only allow you to create managed 
>EMs.
What I'm missing is what happens when there's no JTA tx started (i.e. on 
RENDER_VIEW when one is calling a lazy association, does the em throws an error 
?)
Note : I'm using transaction-type="JTA" on my persistence.xml.

> If you like to use UserTransactions, then check our JPA module. Guess Gerhard 
>added a sample for it. 

In fact, this is my case (I'm using EJBs for 'service' layer).

If I want a an em with transaction-type="JTA" and requestScoped (i.e. not 
having LIE on RENDER_VIEW), I guess I have the following 2 solutions :

Solution 1 : using application managed em
a. have em produced 'a la' softwaremill 
b. create a JSF lifecyclelistener using UserTx, and doing something like :
 * INVOKE_APLICATION#before : userTx.begin()
 * INVOKE_APLICATION#after : userTx.commit() (and test isRollbackOnly)
 * RENDER_VIEW#before : userTx.begin()
 * RENDER_VIEW #after : userTx.rollback()
c. and a servlet Filter for handling exceptional cases in jsf listener (in case 
after is not called) : rollback in those cases
That's a bit overkill for a requestScope em, so I must be missing something.

Solution 2 : use an extendedPersistenceContext with a request scoped statefull 
EJB
In fact, this is so simple, it must be the solution in my case

@Stateful
@RequestScoped
public class EntityManagerProducer {

  @PersistenceContext(type=PersistenceContextType.EXTENDED  private 
EntityManager em;

   @Produces @RequestScoped
   public EntityManager getEntityManager() {
      return em;
  }
} 


Thanks and sorry to deviate from DS stuff - didn't thought so initially !

----- Mail original -----
De : Mark Struberg <[email protected]>
À : "[email protected]" <[email protected]>
Cc : 
Envoyé le : Vendredi 24 août 2012 11h47
Objet : Re: RequestScoped entityManager

Well, we might miss some explanation for this lines


>    @PersistenceContext(unitName="default")
> private EntityManager entityManager;
This only works in SE. In an EE container you will get a Container Managed EM, 
which is not manageable by the user, but strictly bound to EJBs.

Even the sample of softwaremill will not work on every EE container

>    @PersistenceUnit(name="pu")
>    private EntityManagerFactory emf;

On a few servers the EE - injected EMF will only allow you to create managed 
EMs.

The best approach is to either create an additional 
EntityManagerFactoryProducer where you can even stuff in JPA properties in a 
central place, and inject it via CDI:

>    @Inject
>    private EntityManagerFactory emf;
Or you can simply do:

> @ApplicationScoped // important!
> public class EntityManagerProducer {
>    private EntityManagerFactory emf = 
> Persistence.createEntityManagerFactory();
> 
>     @Produces @RequestScoped
>     public EntityManager getEntityManager() {
>         return new EntityManagerTxEnlistDecorator (emf.createEntityManager());
>     }
> 
>     public void close(@Disposes EntityManager em) {
>         em.close();
>     }
> }
> 

Please note that this is the classic way to get a non-JTA EM! If you like to 
use UserTransactions, then check our JPA module. Guess Gerhard added a sample 
for it.

LieGrue,
strub




----- Original Message -----
> From: Adrian Gonzalez <[email protected]>
> To: "[email protected]" 
> <[email protected]>
> Cc: 
> Sent: Friday, August 24, 2012 11:38 AM
> Subject: RequestScoped entityManager
> 
> Hello,
> 
> Does DS provides a requestScopes em ?
> 
> I tried to to something like https://cwiki.apache.org/EXTCDI/jpa-usage.html, 
> but 
> I had an error (don't remember which one, I think it was because em 
> wasn't associated with current tx)
> 
> For the moment, I'm using softwaremill EntityManagerTxEnlistDecorator 
> (https://github.com/softwaremill/softwaremill-common/tree/master/softwaremill-cdi/src/main/java/pl/softwaremill/common/cdi/persistence
> and doing something like (I'm doing it from memory so, there can be some 
> errors) :
> 
> public class EntityManagerProducer {
>    @PersistenceUnit(name="pu")
>    private EntityManagerFactory emf;
> 
>     @Produces @RequestScoped
>     public EntityManager getEntityManager() {
>         return new EntityManagerTxEnlistDecorator (emf.createEntityManager());
>     }
> 
>     public void close(@Disposes EntityManager em) {
>         em.close();
>     }
> }
> 
> Not sure if it deserves a special classe in DS (perhaps at least 
> softwaremill EntityManagerTxEnlistDecorator   ?)
> 
> Thanks
>

Reply via email to