4 Replies Latest reply on Apr 18, 2005 11:16 AM by ceracm

    Accessing the EntityManager from a non-EJB class

    ceracm

      EJBs can obtain a reference to an EntityManager through dependency injection. But how can a non-EJB class called by an EJB access the EntityManager?

      Is it simply a matter of using JNDI code like this?

      EntityManager em = (EntityManager) new InitialContext()
       .lookup("java:comp.ejb3/env/EntityManager");


      This works in preview 5 but is legal and does it perform well?

      Thanks

      Chris


        • 1. Re: Accessing the EntityManager from a non-EJB class
          kabirkhan

          It is fine (apart from possible philosophical discussions :-)

          Note that the spec mentions the EntityManager should be bound under java:comp/env/EntityManager. We are using java:comp.ejb3/env/EntityManage while waiting for the spec to be completed.

          • 2. Re: Accessing the EntityManager from a non-EJB class
            ceracm

            Thanks.

            What theoretical discussions do you have in mind?

            I think its reasonable for a non-EJB to call the EntityManager. For example, if you want to implement a DAO-like class that is called by the session bean.

            It would be nice if a DAO could use dependency injection as well but I assume that it would be too inefficient to make the DAO a session bean as well.

            • 3. Re: Accessing the EntityManager from a non-EJB class
              epbernard

               

              "ceracm" wrote:
              I assume that it would be too inefficient to make the DAO a session bean as well.


              Why?

              • 4. Re: Accessing the EntityManager from a non-EJB class
                ceracm

                 

                "epbernard" wrote:
                "ceracm" wrote:
                I assume that it would be too inefficient to make the DAO a session bean as well.


                Why?


                Thats a very good question.
                One that is perhaps best answered by an application server developer

                I would imagine that calling a session bean through a local interface from another session bean has some overhead.

                But I suppose you could argue that the overhead of the is negligible compared to the work being done by the DAO.

                Comments?