You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltaspike.apache.org by Adrian Gonzalez <ad...@yahoo.fr> on 2012/08/24 11:38:46 UTC

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

Re: RequestScoped entityManager

Posted by Arne Limburg <ar...@openknowledge.de>.
Hi,


I think, all this EntityManager(Factory)Producers should be obsolete with
DeltaSpike.
Instead we should provide a deltaspike-way to configure the EntityManager.
The way Seam-Persistence does this, seems a quite good solution:

@Produces
@DeltaSpikeManaged
@RequestScoped
private EntityManager entityManager;

or

@Produces
@DeltaSpikeManaged
@RequestScoped
@CustomQualified
private EntityManager entityManager;

Thus the user can specify the scope of an EntityManager that is managed by
DeltaSpike.
I just don't know how the user should specify the name of the persistence
unit then.
Three possibilities:
1.
@Produces@DeltaSpikeManaged
@RequestScoped
@CustomQualified
@PersistenceContext(unitName = "myPU")
private EntityManager entityManager;

2.
@Produces@DeltaSpikeManaged
@RequestScoped
@CustomQualified
@Named("myPU")
private EntityManager entityManager;

3.

@Produces@DeltaSpikeManaged
@RequestScoped
@CustomQualified
private EntityManager entityManager;

@Produces

@CustomQualified
@PersistenceUnitName
private String unit = "myPU";


The third solution would have the advantage that one could configure
different unitNames for different ProjectStagesŠ

Cheers,
Arne

Am 27.08.12 21:11 schrieb "Cody Lerum" unter <co...@gmail.com>:

>This is really an area where we should spend some time in the docs
>discussing the pros/cons of each EM approach as well as the endorsed
>ways of setting them in a project.
>
>CODI already has a good start -
>https://cwiki.apache.org/EXTCDI/jpa-usage.html but I'm not sure the
>relationship between each and TX support is clear.
>
>On Sun, Aug 26, 2012 at 5:30 PM, Stuart Douglas <sd...@redhat.com>
>wrote:
>>
>> On 24/08/2012, at 7:47 PM, Mark Struberg <st...@yahoo.de> wrote:
>>
>>> 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.
>>>
>>
>> This is a complete violation of the JPA spec, which containers do this?
>>
>> Stuart
>>
>>
>>> 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 <ad...@yahoo.fr>
>>>> To: "deltaspike-dev@incubator.apache.org"
>>>><de...@incubator.apache.org>
>>>> 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/softwa
>>>>remill-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
>>>>
>>


Re: RequestScoped entityManager

Posted by Cody Lerum <co...@gmail.com>.
This is really an area where we should spend some time in the docs
discussing the pros/cons of each EM approach as well as the endorsed
ways of setting them in a project.

CODI already has a good start -
https://cwiki.apache.org/EXTCDI/jpa-usage.html but I'm not sure the
relationship between each and TX support is clear.

On Sun, Aug 26, 2012 at 5:30 PM, Stuart Douglas <sd...@redhat.com> wrote:
>
> On 24/08/2012, at 7:47 PM, Mark Struberg <st...@yahoo.de> wrote:
>
>> 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.
>>
>
> This is a complete violation of the JPA spec, which containers do this?
>
> Stuart
>
>
>> 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 <ad...@yahoo.fr>
>>> To: "deltaspike-dev@incubator.apache.org" <de...@incubator.apache.org>
>>> 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
>>>
>

Re: RequestScoped entityManager

Posted by Stuart Douglas <sd...@redhat.com>.
On 24/08/2012, at 7:47 PM, Mark Struberg <st...@yahoo.de> wrote:

> 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.
> 

This is a complete violation of the JPA spec, which containers do this? 

Stuart


> 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 <ad...@yahoo.fr>
>> To: "deltaspike-dev@incubator.apache.org" <de...@incubator.apache.org>
>> 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
>> 


Re: RequestScoped entityManager

Posted by Adrian Gonzalez <ad...@yahoo.fr>.
Thanks Mark !
I'll do so then


----- Mail original -----
De : Mark Struberg <st...@yahoo.de>
À : Adrian Gonzalez <ad...@yahoo.fr>; "deltaspike-dev@incubator.apache.org" <de...@incubator.apache.org>
Cc : 
Envoyé le : Vendredi 24 août 2012 13h49
Objet : Re: RequestScoped entityManager

If you are using EJB and EE then you should be fine with just using  an Extended EM.

LieGrue,
strub




----- Original Message -----
> From: Adrian Gonzalez <ad...@yahoo.fr>
> To: "deltaspike-dev@incubator.apache.org" <de...@incubator.apache.org>; Mark Struberg <st...@yahoo.de>
> Cc: 
> Sent: Friday, August 24, 2012 12:54 PM
> Subject: Re: RequestScoped entityManager
> 
>>  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 <st...@yahoo.de>
> À : "deltaspike-dev@incubator.apache.org" 
> <de...@incubator.apache.org>
> 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 <ad...@yahoo.fr>
>>  To: "deltaspike-dev@incubator.apache.org" 
> <de...@incubator.apache.org>
>>  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
>> 
>


Re: RequestScoped entityManager

Posted by Mark Struberg <st...@yahoo.de>.
If you are using EJB and EE then you should be fine with just using  an Extended EM.

LieGrue,
strub




----- Original Message -----
> From: Adrian Gonzalez <ad...@yahoo.fr>
> To: "deltaspike-dev@incubator.apache.org" <de...@incubator.apache.org>; Mark Struberg <st...@yahoo.de>
> Cc: 
> Sent: Friday, August 24, 2012 12:54 PM
> Subject: Re: RequestScoped entityManager
> 
>>  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 <st...@yahoo.de>
> À : "deltaspike-dev@incubator.apache.org" 
> <de...@incubator.apache.org>
> 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 <ad...@yahoo.fr>
>>  To: "deltaspike-dev@incubator.apache.org" 
> <de...@incubator.apache.org>
>>  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
>> 
> 

Re: RequestScoped entityManager

Posted by Adrian Gonzalez <ad...@yahoo.fr>.
> 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 <st...@yahoo.de>
À : "deltaspike-dev@incubator.apache.org" <de...@incubator.apache.org>
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 <ad...@yahoo.fr>
> To: "deltaspike-dev@incubator.apache.org" <de...@incubator.apache.org>
> 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
>


Re: RequestScoped entityManager

Posted by Mark Struberg <st...@yahoo.de>.
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 <ad...@yahoo.fr>
> To: "deltaspike-dev@incubator.apache.org" <de...@incubator.apache.org>
> 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
>