@New and producer methods/fields

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

@New and producer methods/fields

Mark Struberg
Hi folks!

We recently talked about ways to properly destroy beans which got created via Instance or @New.
I just realized that only having
@Inject @New MyClass dings;
might not be enough.

Imagine you have 2 producer methods which create EntityManagers

public class EntityManagerProducer {  
    @Produces @RequestScoped @UserDb
    public void createUserDbEm() {
        return entityManagerFactory.createEntityManager("userdb");
    }     
   
    @Produces @RequestScoped @AdminDb
    public void createAdminDbEm() {
        return entityManagerFactory.createEntityManager("admindb");
    }     
}

If I need a 'temporarily self managed' userdb EntityManager, I cannot just
type
@Inject @New @UserDb EntityManager userDbEm;
because according to the spec there is only 1 Bean with exactly @New (and none with additional @UserDb)
The @New is basically useless for producer methods, isn't?

Do we like to address this somehow?

LieGrie,
strub


_______________________________________________
cdi-dev mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/cdi-dev
Reply | Threaded
Open this post in threaded view
|

Re: @New and producer methods/fields

Pete Muir
Administrator
Section 3.12 states "For each managed bean, and for each session bean, a second bean exists which:"

So, there are no @New beans for producers.

What is the use case you are trying to address?

On 24 Sep 2011, at 11:35, Mark Struberg wrote:

> Hi folks!
>
> We recently talked about ways to properly destroy beans which got created via Instance or @New.
> I just realized that only having
> @Inject @New MyClass dings;
> might not be enough.
>
> Imagine you have 2 producer methods which create EntityManagers
>
> public class EntityManagerProducer {  
>     @Produces @RequestScoped @UserDb
>     public void createUserDbEm() {
>         return entityManagerFactory.createEntityManager("userdb");
>     }    
>    
>     @Produces @RequestScoped @AdminDb
>     public void createAdminDbEm() {
>         return entityManagerFactory.createEntityManager("admindb");
>     }    
> }
>
> If I need a 'temporarily self managed' userdb EntityManager, I cannot just
> type
> @Inject @New @UserDb EntityManager userDbEm;
> because according to the spec there is only 1 Bean with exactly @New (and none with additional @UserDb)
> The @New is basically useless for producer methods, isn't?
>
> Do we like to address this somehow?
>
> LieGrie,
> strub
>
>
> _______________________________________________
> cdi-dev mailing list
> [hidden email]
> https://lists.jboss.org/mailman/listinfo/cdi-dev


_______________________________________________
cdi-dev mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/cdi-dev
Reply | Threaded
Open this post in threaded view
|

Re: @New and producer methods/fields

Mark Struberg
As explained below: in every case where you need a producer method to create a useful bean, then @New just doesn't work.


My usecase was that I would have needed one of those EntityManagers for unit tests. And since unit tests are no CDI managed beans thus cannot use @Transactional, I wanted to just create a manually managed contextual instance.

@New currently is basically only useful for creating new instances of a class (which must not even be a managed bean but vetoed...).


LieGrue,
strub



----- Original Message -----

> From: Pete Muir <[hidden email]>
> To: Mark Struberg <[hidden email]>
> Cc: "[hidden email]" <[hidden email]>
> Sent: Sunday, September 25, 2011 9:56 PM
> Subject: Re: [cdi-dev] @New and producer methods/fields
>
> Section 3.12 states "For each managed bean, and for each session bean, a
> second bean exists which:"
>
> So, there are no @New beans for producers.
>
> What is the use case you are trying to address?
>
> On 24 Sep 2011, at 11:35, Mark Struberg wrote:
>
>>  Hi folks!
>>
>>  We recently talked about ways to properly destroy beans which got created
> via Instance or @New.
>>  I just realized that only having
>>  @Inject @New MyClass dings;
>>  might not be enough.
>>
>>  Imagine you have 2 producer methods which create EntityManagers
>>
>>  public class EntityManagerProducer { 
>>      @Produces @RequestScoped @UserDb
>>      public void createUserDbEm() {
>>          return
> entityManagerFactory.createEntityManager("userdb");
>>      }   
>>    
>>      @Produces @RequestScoped @AdminDb
>>      public void createAdminDbEm() {
>>          return
> entityManagerFactory.createEntityManager("admindb");
>>      }   
>>  }
>>
>>  If I need a 'temporarily self managed' userdb EntityManager, I
> cannot just
>>  type
>>  @Inject @New @UserDb EntityManager userDbEm;
>>  because according to the spec there is only 1 Bean with exactly @New (and
> none with additional @UserDb)
>>  The @New is basically useless for producer methods, isn't?
>>
>>  Do we like to address this somehow?
>>
>>  LieGrie,
>>  strub
>>
>>
>>  _______________________________________________
>>  cdi-dev mailing list
>>  [hidden email]
>>  https://lists.jboss.org/mailman/listinfo/cdi-dev
>

_______________________________________________
cdi-dev mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/cdi-dev
Reply | Threaded
Open this post in threaded view
|

Re: @New and producer methods/fields

Pete Muir
Administrator

On 25 Sep 2011, at 21:10, Mark Struberg wrote:

> As explained below: in every case where you need a producer method to create a useful bean, then @New just doesn't work.
>
>
> My usecase was that I would have needed one of those EntityManagers for unit tests. And since unit tests are no CDI managed beans thus cannot use @Transactional, I wanted to just create a manually managed contextual instance.

It wasn't intended for this use case, but we could consider if this is possible.

File an issue, I have a bad feeling there is a gotcha in here somewhere, but not sure atm.

>
> @New currently is basically only useful for creating new instances of a class (which must not even be a managed bean but vetoed...).

This is the use case for which it is intended.

>
>
> LieGrue,
> strub
>
>
>
> ----- Original Message -----
>> From: Pete Muir <[hidden email]>
>> To: Mark Struberg <[hidden email]>
>> Cc: "[hidden email]" <[hidden email]>
>> Sent: Sunday, September 25, 2011 9:56 PM
>> Subject: Re: [cdi-dev] @New and producer methods/fields
>>
>> Section 3.12 states "For each managed bean, and for each session bean, a
>> second bean exists which:"
>>
>> So, there are no @New beans for producers.
>>
>> What is the use case you are trying to address?
>>
>> On 24 Sep 2011, at 11:35, Mark Struberg wrote:
>>
>>> Hi folks!
>>>
>>> We recently talked about ways to properly destroy beans which got created
>> via Instance or @New.
>>> I just realized that only having
>>> @Inject @New MyClass dings;
>>> might not be enough.
>>>
>>> Imagine you have 2 producer methods which create EntityManagers
>>>
>>> public class EntityManagerProducer {  
>>>      @Produces @RequestScoped @UserDb
>>>      public void createUserDbEm() {
>>>          return
>> entityManagerFactory.createEntityManager("userdb");
>>>      }    
>>>    
>>>      @Produces @RequestScoped @AdminDb
>>>      public void createAdminDbEm() {
>>>          return
>> entityManagerFactory.createEntityManager("admindb");
>>>      }    
>>> }
>>>
>>> If I need a 'temporarily self managed' userdb EntityManager, I
>> cannot just
>>> type
>>> @Inject @New @UserDb EntityManager userDbEm;
>>> because according to the spec there is only 1 Bean with exactly @New (and
>> none with additional @UserDb)
>>> The @New is basically useless for producer methods, isn't?
>>>
>>> Do we like to address this somehow?
>>>
>>> LieGrie,
>>> strub
>>>
>>>
>>> _______________________________________________
>>> cdi-dev mailing list
>>> [hidden email]
>>> https://lists.jboss.org/mailman/listinfo/cdi-dev
>>


_______________________________________________
cdi-dev mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/cdi-dev
Reply | Threaded
Open this post in threaded view
|

Re: @New and producer methods/fields

Mark Struberg
filed under CDI-170.

have fun :)

LieGrue,
strub



----- Original Message -----

> From: Pete Muir <[hidden email]>
> To: Mark Struberg <[hidden email]>
> Cc: "[hidden email]" <[hidden email]>
> Sent: Sunday, September 25, 2011 10:11 PM
> Subject: Re: [cdi-dev] @New and producer methods/fields
>
>
> On 25 Sep 2011, at 21:10, Mark Struberg wrote:
>
>>  As explained below: in every case where you need a producer method to
> create a useful bean, then @New just doesn't work.
>>
>>
>>  My usecase was that I would have needed one of those EntityManagers for
> unit tests. And since unit tests are no CDI managed beans thus cannot use
> @Transactional, I wanted to just create a manually managed contextual instance.
>
> It wasn't intended for this use case, but we could consider if this is
> possible.
>
> File an issue, I have a bad feeling there is a gotcha in here somewhere, but not
> sure atm.
>
>>
>>  @New currently is basically only useful for creating new instances of a
> class (which must not even be a managed bean but vetoed...).
>
> This is the use case for which it is intended.
>
>>
>>
>>  LieGrue,
>>  strub
>>
>>
>>
>>  ----- Original Message -----
>>>  From: Pete Muir <[hidden email]>
>>>  To: Mark Struberg <[hidden email]>
>>>  Cc: "[hidden email]" <[hidden email]>
>>>  Sent: Sunday, September 25, 2011 9:56 PM
>>>  Subject: Re: [cdi-dev] @New and producer methods/fields
>>>
>>>  Section 3.12 states "For each managed bean, and for each session
> bean, a
>>>  second bean exists which:"
>>>
>>>  So, there are no @New beans for producers.
>>>
>>>  What is the use case you are trying to address?
>>>
>>>  On 24 Sep 2011, at 11:35, Mark Struberg wrote:
>>>
>>>>  Hi folks!
>>>>
>>>>  We recently talked about ways to properly destroy beans which got
> created
>>>  via Instance or @New.
>>>>  I just realized that only having
>>>>  @Inject @New MyClass dings;
>>>>  might not be enough.
>>>>
>>>>  Imagine you have 2 producer methods which create EntityManagers
>>>>
>>>>  public class EntityManagerProducer { 
>>>>       @Produces @RequestScoped @UserDb
>>>>       public void createUserDbEm() {
>>>>           return
>>>  entityManagerFactory.createEntityManager("userdb");
>>>>       }   
>>>>    
>>>>       @Produces @RequestScoped @AdminDb
>>>>       public void createAdminDbEm() {
>>>>           return
>>>  entityManagerFactory.createEntityManager("admindb");
>>>>       }   
>>>>  }
>>>>
>>>>  If I need a 'temporarily self managed' userdb
> EntityManager, I
>>>  cannot just
>>>>  type
>>>>  @Inject @New @UserDb EntityManager userDbEm;
>>>>  because according to the spec there is only 1 Bean with exactly
> @New (and
>>>  none with additional @UserDb)
>>>>  The @New is basically useless for producer methods, isn't?
>>>>
>>>>  Do we like to address this somehow?
>>>>
>>>>  LieGrie,
>>>>  strub
>>>>
>>>>
>>>>  _______________________________________________
>>>>  cdi-dev mailing list
>>>>  [hidden email]
>>>>  https://lists.jboss.org/mailman/listinfo/cdi-dev
>>>
>

_______________________________________________
cdi-dev mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/cdi-dev