EJB references


EJB design: EJB references

  1. EJB references (6 messages)

    I have a number of questions regarding EJB references in deployment descriptors (EJB 1.1):

    1) should EJB references be used only when you have an aggregate EJB? OR should you also use it when/if one of your EJB methods instantiates another EJB?

    2) what is the meaning and purpose of the ejb-link tag? I've read in Borland JBuilder's documentation contradictory statements that it is only used when you're referencing an EJB in the same jar but this isn't my reading of the EJB 1.1 specification. So could someone clarify what it means and when you should use it?

    3) should you use EJB references when the 'referred to' EJB is remote, i.e. sitting in another application server? If so, how do you specify the INITIAL_CONTEXT_FACTORY and PROVIDER_URL properties that are usually passed to InitialContext by an EJB client. An example deployment descriptor would be very welcome here.


    Threaded Messages (6)

  2. EJB references[ Go to top ]

    Hi Paul,

    1) Use ejb-ref in both cases, viz aggregate EJB and EJB-EJB

    2) <ejb-link> tag

    The J2EE Application Assembler can use the <ejb-link> element in the deployment descriptor to link an EJB reference to a target enterprise bean. The link will be observed by the deployment tools. The Application Assembler specifies the link between two enterprise beans as follows:
       &#8226; The Application Assembler uses the optional ejb-link element of the ejb-ref element of the referencing enterprise bean. The value of the ejb-link element is the name of the target enterprise bean. (It is the name defined in the ejb-name element of the target enterprise
    bean.) The target enterprise bean can be in the same ejb-jar file, or in another ejb-jar in the same J2EE application unit as the referencing enterprise bean.
       &#8226; The Application Assembler must ensure that the target enterprise bean is type-compatible with
    the declared EJB reference. This means that the target enterprise bean must be of the type indi-cated
    in the ejb-ref-type element, and that the home and remote interfaces of the target enterprise bean must be Java type-compatible with the interfaces declared in the EJB reference.

       The following illustrates an ejb-link in the deployment descriptor.

        The Application Assembler uses the ejb-link element to indicate that the EJB reference &#8220;EmplRecord&#8221; declared in the EmployeeService enterprise bean has been linked to the EmployeeRecord enterprise bean.


    3. They above point would have slightly answered this question. ejb-ref's are useful only when the beans are inside the same JVM (app.server's container). If the second bean is deployed in the other container, then u've to do normal JNDI lookup.

    (ahamed at aztec dot soft dot net)
  3. EJB references[ Go to top ]

    Thanks for your reply, Ahmed.

    This is my fault, not yours, but I'm still a little unclear why anyone would use ejb-link. The target EJB's home and remote interfaces are already specified in the ejb-ref tags, so why use an ejb-link tag aswell?

  4. EJB references[ Go to top ]

    This tag is only optional. In any j2ee application there is a process called "application assembly". The application assembler takes all the individual components (beans) and assembles them inorder to form the complete application. In that process he can make use of some GUI tools provided by that appserver. In that he can easily establish an assciation between the source and target bean, only with the name of the other bean which we gave using <ejb-link> tag.

    (ahamed at aztec dot soft dot net)
  5. EJB references[ Go to top ]

    I am slightly confused, Ahmed. Are you saying (in point 3) that if I have my ejb-ref and ejb-link defined in my deployment desc, I do not have to do a JNDI lookup of that associated bean? If so, could you tell me how can I get a reference to the associated bean's remote interface in this case (couple of lines of code would be really helpful).
    Thank you
  6. EJB references[ Go to top ]

    Hi Anuj,

    I'm giving code for normal JNDI lookup and using <ejb-ref>. This code will be present in the source bean and let the target bean be Employee.

    1. Normal JNDI lookup:-

       Properties props = new Properties();
       props.put(Context.SECURITY_PRINCIPAL, username);
       props.put(Context.SECURITY_CREDENTIAL, password);

       // 'props' now contains information about the
       // naming service running in a remote machine.
       // Acquire the reference to the root context
       // of that naming service (a tree like data structure)
       Context ctx = new InitialContext(props);

       // look up the object that was bound to this ctx
       EmployeeHome home = (EmployeeHome)ctx.lookup(jndiName);

    2. Using ejb-ref:-

       It's fairly simple. If the beans are in the same JVM, then why should we go to the Naming server to get reference to the other bean. Instead ask the container to give u the reference to the other bean.

        Contect ctx = new InitialContext();
        // see the constructor for InitialContext

        EmployeeHome home = (EmployeeHome)ctx.lookup(

    "ejb/employee" is the name of the reference which we gave in the ejb-jar.xml file of the first bean.

    (ahamed at aztec dot soft dot net)
  7. EJB references[ Go to top ]

    EJB 1.1 reference was a solution for solving simple relation problem.
    Since, the EJB 2.0 specification has really pushed in the relation management.
    So, I don't think it's a good idea to use reference now. If you plan a possible migration in the next future, I really think it's better to delay any kind of relation management (through helper) and to wait for a compliant EJB 2.0 app server.
    Any though ?

    Stephane Valsemay.