Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Critical Critical
    • Resolution: Cannot Reproduce
    • Affects Version/s: V3
    • Fix Version/s: 3.1
    • Component/s: cdi
    • Labels:
      None
    • Environment:

      Operating System: All
      Platform: All

    • Issuezilla Id:
      11,450

      Description

      According to the CDI spec, the following should work (but doesn't):

      public class MyResourceFactory {
      @Producer @PersistenceContext(unitName="MyPersistenceUnit") EnityManager em;
      }

      @Stateless
      public class MyEjbLight {
      @Inject EntityManager em;

      public void accessEm() 

      { em.find(CustomerOrder.class) // this fails with the exception copied bellow. }

      }

      It fails with: "Caused by: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for
      unitName MyPersistenceUnit"

      It works if I replace "@Inject EntityManager em;" with "@PersistenceContext EntityManager em;".

      More-over: as long as the top-most ejb in a call graph has @PersistenceContext EntityManager em;"
      then all other CDI beans in the call graph may use "@Inject EntityManager em;" (assuming the
      @Producer is declared as above).

      /Johan

        Activity

        Hide
        johaneltes added a comment -

        I should add that I invoke the ejb3 light ejb (the topmost EJB of the call graph) from JUnit using the
        following code:

        container = EJBContainer.createEJBContainer();
        Context ctx = container.getContext();
        MyEjbLight ejb = (MyEjbLight)ctx.lookup("java:global/classes/MyEjbLight");
        ejb.accessEm();

        Show
        johaneltes added a comment - I should add that I invoke the ejb3 light ejb (the topmost EJB of the call graph) from JUnit using the following code: container = EJBContainer.createEJBContainer(); Context ctx = container.getContext(); MyEjbLight ejb = (MyEjbLight)ctx.lookup("java:global/classes/MyEjbLight"); ejb.accessEm();
        Hide
        ksak added a comment -

        Does the same code work when deployed to the server? Does it work in the EJB Embeddable API case if
        the @PersistenceContext declaration does not specify unitName? Also, please include the complete
        stack trace.

        Show
        ksak added a comment - Does the same code work when deployed to the server? Does it work in the EJB Embeddable API case if the @PersistenceContext declaration does not specify unitName? Also, please include the complete stack trace.
        Hide
        choicegh added a comment -

        This should also work in non-ejbs according to the spec but doesn't also.

        public class MyResourceFactory

        { @Producer @PersistenceContext(unitName="MyPersistenceUnit") EnityManager em; }

        @Named
        public class MyBean {
        @Inject EntityManager em;

        public void accessEm()

        { em.find(CustomerOrder.class) // this fails with the exception copied bellow. }

        }

        Show
        choicegh added a comment - This should also work in non-ejbs according to the spec but doesn't also. public class MyResourceFactory { @Producer @PersistenceContext(unitName="MyPersistenceUnit") EnityManager em; } @Named public class MyBean { @Inject EntityManager em; public void accessEm() { em.find(CustomerOrder.class) // this fails with the exception copied bellow. } }
        Hide
        marina vatkina added a comment -

        Doesn't seem to be ejb container issue

        Show
        marina vatkina added a comment - Doesn't seem to be ejb container issue
        Hide
        Mitesh Meswani added a comment -

        Assigning to Siva for initial investigation..

        Show
        Mitesh Meswani added a comment - Assigning to Siva for initial investigation..
        Hide
        rwappler added a comment -
            • Issue 11450 has been confirmed by votes. ***
        Show
        rwappler added a comment - Issue 11450 has been confirmed by votes. ***
        Hide
        rwappler added a comment -

        The issue also occurs on the server. Details are as follows:

        @Stateful public class EjbA {
        @EJB EjbB;

        public void persist()

        { EjbB.persit(); }

        }

        @Stateful public class EjbB {

        @Inject @MyPU EntityManager em;

        private MyEntity entity = new MyEntity();

        public void persist()

        { entity = em.merge(entity); ... }

        }

        The qualifier for the producer field:
        @Produces @MyPu @PersistenceContext(unitName="my-pu") EntityManager em;

        When the servlet calls EjbA.persist(), a NPE occurs at the line, where EjbB
        class em.merge(entity).

        The code in the servlet utilises jakarta-cactus, thus the lookup of EjbA is done
        via JNDI in a newly constructed InitialContext instead of injection.

        Interestingly, the injection of the entity manager with the same code as above
        works in EjbA and the entity manager is not null.

        I hope that helps.

        Show
        rwappler added a comment - The issue also occurs on the server. Details are as follows: @Stateful public class EjbA { @EJB EjbB; public void persist() { EjbB.persit(); } } @Stateful public class EjbB { @Inject @MyPU EntityManager em; private MyEntity entity = new MyEntity(); public void persist() { entity = em.merge(entity); ... } } The qualifier for the producer field: @Produces @MyPu @PersistenceContext(unitName="my-pu") EntityManager em; When the servlet calls EjbA.persist(), a NPE occurs at the line, where EjbB class em.merge(entity). The code in the servlet utilises jakarta-cactus, thus the lookup of EjbA is done via JNDI in a newly constructed InitialContext instead of injection. Interestingly, the injection of the entity manager with the same code as above works in EjbA and the entity manager is not null. I hope that helps.
        Hide
        rwappler added a comment -

        Arg, please forget my last comment. The reason was totally different. My beans
        exposed a no-interface view and EjbB.persist() was not public. Nevertheless, the
        plain NPE is not helpful and a more useful error message would help.

        Show
        rwappler added a comment - Arg, please forget my last comment. The reason was totally different. My beans exposed a no-interface view and EjbB.persist() was not public. Nevertheless, the plain NPE is not helpful and a more useful error message would help.
        Hide
        Sivakumar Thyagarajan added a comment -

        I couldn't reproduce this scenario. Please see the developer tests (em-injection-
        no-interface-ejb and em-resource-injection) that I checked in as part of revision
        41488.

        EntityManager injection into a Statless bean and a Servlet that was produced
        through a Producer field seems to work in 3.1 latest build. If you still could
        reproduce this issue, please reopen this issue with a reproducible test-case.
        Thanks

        Show
        Sivakumar Thyagarajan added a comment - I couldn't reproduce this scenario. Please see the developer tests (em-injection- no-interface-ejb and em-resource-injection) that I checked in as part of revision 41488. EntityManager injection into a Statless bean and a Servlet that was produced through a Producer field seems to work in 3.1 latest build. If you still could reproduce this issue, please reopen this issue with a reproducible test-case. Thanks

          People

          • Assignee:
            Sivakumar Thyagarajan
            Reporter:
            johaneltes
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: