deltaspike-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stuart Douglas <sdoug...@redhat.com>
Subject Re: RequestScoped entityManager
Date Sun, 26 Aug 2012 23:30:38 GMT

On 24/08/2012, at 7:47 PM, Mark Struberg <struberg@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 <adr_gonzalez@yahoo.fr>
>> To: "deltaspike-dev@incubator.apache.org" <deltaspike-dev@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
>> 


Mime
View raw message