1 2 Previous Next 17 Replies Latest reply: Mar 9, 2010 7:20 PM by Pete Muir RSS

    Entity Manager Producer Revisited

    John Leed Newbie

      I think I asked this in another thread, but never got a clear answer.


      I have an EntityManager and EntityManagerFactory producer bean, as suggested in section 14.1 of the Weld documentation. It's rather simple:


      @Stateless
      public class IntranetDatabaseProducer {
          @Produces @IntranetDatabase @PersistenceContext(unitName="IntranetPU")
          EntityManager entityManager;
          
          @Produces @IntranetDatabase @PersistenceUnit(unitName="IntranetPU")
          EntityManagerFactory entityManagerFactory;
      }
      



      The only way I can get it to work with anything though is to make it a SLSB. If I make it @SessionScoped or @ApplicationScoped, nothing gets produced and no exceptions are thrown until something tries to use the em reference.


      If I change things around from a field producer to a method producer, I can see it isn't that nothing is being injected but rather that null is being produced. I see output in the log from the producer method, which means that resource injection isn't working here.


      @SessionScoped
      public class IntranetDatabaseProducer implements Serializable {
          @PersistenceContext(unitName="IntranetPU")
          EntityManager entityManager;
      
          @Produces @IntranetDatabase
          public EntityManager produceEntityManager() {
              System.out.println("Producing an entity manager");
              return entityManager;
          }
      }
      



      So what am I missing here? For reference, I'm running Weld 1.0.1-Final on Glassfish v3 full profile.

        • 1. Re: Entity Manager Producer Revisited
          Nicklas Karlsson Master

          Hmm. Can you try it in a JBoss AS 6.0.0.M2? Just trying to pinpoint if it's in Weld or the GF - Weld integration for @PersistenceContext resolves

          • 2. Re: Entity Manager Producer Revisited
            John Leed Newbie

            I think that's going to take a while... I've never used JBoss.

            • 3. Re: Entity Manager Producer Revisited
              Nicklas Karlsson Master

              It is tested in the TCK with


              http://is.gd/9tLFD


              which looks pretty similar.

              • 4. Re: Entity Manager Producer Revisited
                Gavin King Master

                I would take that as a bug in GlassFish.

                • 5. Re: Entity Manager Producer Revisited
                  Matthieu Chase Heimer Newbie

                  Page 28 of the CDI spec states: The container is not required to support resources with scope other than @Dependent. Portable applications should not define resources with any scope other than @Dependent.


                  I tested an EntityManager and EntityManagerFactory producer with GlassFish and Weld 1.0 and unless I use (or default) my producer to @Dependent I get org.jboss.weld.NullInstanceException: WELD-000044 Unable to obtain instance from org.jboss.weld.bean. Using @Dependent makes it work fine.

                  • 6. Re: Entity Manager Producer Revisited
                    John Leed Newbie

                    That's odd... I submitted this reply yesterday and it didn't take.


                    I tested the combinations of @Dependent with this and think I covered it. When tried field producers with dependent scope by default or with @Dependent on the bean or the fields, I got a WELD-000044 on page load. When I tried the same thing but with method producers, I got a NullPointerException instead.


                    Either way, it doesn't look like the resource is getting injected by the container.

                    • 7. Re: Entity Manager Producer Revisited
                      Matthieu Chase Heimer Newbie
                      Stupid question: Are you sure it is a CDI/Weld issue? Can you do a standard EE injection of @PersistenceUnit(unitName="IntranetPU") EntityManagerFactory entityManagerFactory; ? Add a servlet or ejb to your project to test with.

                      Can you show your code with the dependent producer and the injection point?
                      • 8. Re: Entity Manager Producer Revisited
                        John Leed Newbie

                        It's not a stupid question. I was a PC tech years ago and with some people you couldn't even take it for granted that the machine was on.


                        I had everything set up on the standard EE injection before with Weld enabled. There were a couple of times when I switched back to it when having trouble with this issue.


                        Like I said, I tried plenty of variations for the producer. There's the method:


                        @PersistenceContext(unitName="IntranetPU")
                        EntityManager entityManager;
                        
                        @Produces @IntranetDatabase
                        public EntityManager produceEntityManager() {
                            return entityManager;
                        }
                        
                        public void disposeEntityManager(@Disposes @IntranetDatabase EntityManager em) {}
                        



                        and the field:


                        @Produces @IntranetDatabase @PersistenceContext(unitName="IntranetPU")
                        EntityManager entityManager;
                        



                        I've tried @Dependent on the field / method, on the bean and no where at all. None of them work.


                        As for the injection points, I have them in multitude of locations: @SessionScoped backing beans, @ConversationScoped backing beans, JAX-RS resources, EJB's... if the container creates it, I inject into it! The vast majority (if not all) of the injection points for the qualifier are:


                        @Inject @IntranetDatabase EntityManager em; 
                        





                        • 9. Re: Entity Manager Producer Revisited
                          Matthieu Chase Heimer Newbie

                          Still working for me on GlassFish Full Profile w/ Weld 1.0.


                          Your Producer class itself is dependent scope right?


                          @IntranetDatabase is just a qualifier and not a stereotype correct?


                          If you want, put together a small project for download that has the problem and I'll test.


                          Did you patch the GlassFish code yourself to get 1.0.1? I'm starting to wonder if the problem is only a 1.0.1 and GFv3 issue....

                          • 10. Re: Entity Manager Producer Revisited
                            John Leed Newbie

                            Yes, I patched the integration module to get 1.0.1 to work. It's only a two line fix: https://glassfish.dev.java.net/issues/show_bug.cgi?id=11435


                            I tested every @Dependent use I could think of, on the bean, on the producer, by default with no annotation. Results only differed with using a field or method producer.


                            My qualifier is:


                            @Qualifier
                            @Retention(RetentionPolicy.RUNTIME)
                            @Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
                            public @interface IntranetDatabase {}
                            



                            Like I said, if I just annotate the bean @Stateless everything works fine.

                            • 11. Re: Entity Manager Producer Revisited
                              John Leed Newbie

                              Okay, I think I understand what the problem is. I keep my entity classes along with that producer and qualifier in a common jar to use in my applications. If I move the producer and qualifier into the war's classes, then @Dependent works fine.


                              So, why doesn't resource injection work for an @Dependent bean in a bean archive?


                              Before anyone asks: yes, the archive does have a beans.xml in it; if it didn't the @Stateless version wouldn't work either.

                              • 12. Re: Entity Manager Producer Revisited
                                Pete Muir Master

                                John Leed wrote on Mar 04, 2010 17:30:


                                Okay, I think I understand what the problem is. I keep my entity classes along with that producer and qualifier in a common jar to use in my applications. If I move the producer and qualifier into the war's classes, then @Dependent works fine.

                                So, why doesn't resource injection work for an @Dependent bean in a bean archive?

                                Before anyone asks: yes, the archive does have a beans.xml in it; if it didn't the @Stateless version wouldn't work either.


                                This (not enabling the common jar) is a bug in GlassFish, please file an issue in their issue tracker. I know GlassFish has a number of issues around what is a bean archive...

                                • 13. Re: Entity Manager Producer Revisited
                                  Pete Muir Master

                                  Matthieu Heimer wrote on Mar 02, 2010 07:20:


                                  Page 28 of the CDI spec states: The container is not required to support resources with scope other than @Dependent. Portable applications should not define resources with any scope other than @Dependent.

                                  I tested an EntityManager and EntityManagerFactory producer with GlassFish and Weld 1.0 and unless I use (or default) my producer to @Dependent I get org.jboss.weld.NullInstanceException: WELD-000044 Unable to obtain instance from org.jboss.weld.bean. Using @Dependent makes it work fine.


                                  Matthieu, could you file an issue for this one, we should at least give you a nicer error message that explains the problem properly!

                                  • 14. Re: Entity Manager Producer Revisited
                                    John Leed Newbie

                                    Pete Muir wrote on Mar 09, 2010 11:43:


                                    This (not enabling the common jar) is a bug in GlassFish, please file an issue in their issue tracker. I know GlassFish has a number of issues around what is a bean archive...


                                    Thanks for the info Pete. I filed https://glassfish.dev.java.net/issues/show_bug.cgi?id=11640 last week for the @Dependent em producer. From the other issues, it does seem it has trouble with bean archive definition. Is that tested for in the TCK?

                                    1 2 Previous Next