Discussions

EJB programming & troubleshooting: Problem trying to lookup Home Object

  1. Problem trying to lookup Home Object (4 messages)

    Hi everybody!!!
    We got a problem with JBuilder 6.0 Enterprise trying to lookup the home object.
     
    If we make the search in this way:
        Context ctx = new InitialContext();
        Object red = ctx.lookup("Teller");
    everything works fine, but, if we use:
        Context ctx = new InitialContext();
        Object ref = ctx.lookup("java:comp/env/ejb/Teller");
    just like we saw on tutorials and samples fails with "NameNotFound" exception.
     
    Why is the lookup different in this environment (JBuilder+BES)? If this is the right way, it´s portable to other application servers?
     
    Thanks...
  2. have you define the environment variable in the deployment descriptor?
  3. Problem trying to lookup Home Object[ Go to top ]

    Sorry for the stupid question I'm very new to this topic. Which environment variable do you mean?
  4. Problem trying to lookup Home Object[ Go to top ]

    The lookup is in a JavaBen object invoked from a JSP page.
    In prior samples (with "java:comp/env/ejb/Teller" in RI of J2EE from SUN) i never have define an environment variable in the DD and everything work fine.
    What is the environment varible in DD?????

    PD: sorry for my english, please :)
  5. Hi Mauro,

    Your problem is not specific to JBuilder+BES, it has to do with the concept of JNDI namespaces in J2EE:

    When you assemble & deploy an application you configure JNDI names for resources such as data sources, EJB Home objects, etc. The JNDI names configured for these resources are placed in a *global* JNDI space. In your code sample, the *global* JNDI name of your EJB is "Teller".

    Now, each application also has a "private" or *local* JNDI namespace, with the root: "java:comp/env/". However, objects referenced in the *global* JNDI namespace are not automatically copied into the *local* JNDI namespace of applications. To copy a reference from the global JNDI namespace to the local JNDI namespace of an application (so that you can look it up through "java:comp/env/"), you'll need to add a specific section to the application deployment descriptor (web.xml), to declare the corresponding reference.

    Once this reference declaration has been added, the application can look up the corresponding reference, both in the JNDI global namespace and in its local JNDI context.

    In your application, you therefore need to do the following:

    - first of all, to be more in line with J2EE standards, I would recommend you change the JNDI name of your EJB ("Teller") to "ejb/Teller". The use of the "ejb/" prefix here helps you organise your global JNDI namespace in a nice & clean way: all EJB JNDI names should be prefixed with "ejb/", and all declarations of data sources should be prefixed with "jdbc/".


    - then, you need to edit the deployment descriptor for you Web application (WEB-INF/web.xml) and add an EJB reference section (as shown below). This will cause the reference to the EJB placed in the global JNDI context ("ejb/Teller") to be copied to your application's local JNDI context (with the name "java:comp/env/ejb/Teller") so it can be accessed from either its local or global JNDI name:
    ______________

    <ejb-ref>
        <ejb-ref-name>ejb/Teller</ejb-ref-name>
        <ejb-ref-type>Session</ejb-ref-type>
        <home>com.yourcompany.your_ejb_package.TellerHome</home>
        <remote>com.yourcompany.your_ejb_package.Teller</remote>
        <ejb-link>Teller</ejb-link>
    </ejb-ref>
    ____________

    Where "com.yourcompany.your_ejb_package" is the package that your EJB belongs to, and "TellerHome" and "Teller" are respectively your EJB's home interface and remote interface.

    Hope this will help you get a better grasp of how things work. I'd recommend you also study J2EE tutorials availabble online on Sun's Web site and download & read the specifications for JNDI and EJB... Although the specs are not exactly what I would call "crystal-clear", and some sections lack clarity, they still stand as the "reference" & primary source of information...

    Regards,

    Laurent Denanot