13.3.2 Binding EJB and resource references
An EJB client can define EJB references, logical names, or nicknames, used by the client to find the EJB homes. When using references, the client can hard code the name of the reference. Then, during deployment, the reference is mapped to the real name in the JNDI namespace to which the EJB is bound. A reference to an EJB specifies either the local or remote home of the EJB.
For remote home interfaces, using EJB references is an option, but also a best practice. For local home interfaces, however, it is a must, because using an EJB reference is the only way an EJB client can look up a local home interface.
J2EE applications also use other kinds of references, such as resource references to look up data sources.
Table 13-2 lists some of the EJB references used by the Plants by WebSphere sample.
Table 13-2
EJB name Reference name JNDI Name BackOrder ejb/Inventory plantsby/InventoryHome BackOrder ejb/IdGenerator plantsby/IdGeneratorHome Inventory ejb/BackOrderStock plantsby/BackOrderStockHome Order ejb/OrderItem plantsby/OrderItemHome Order ejb/IdGenerator plantsby/IdGeneratorHome Login Session EJB ejb/Customer plantsby/CustomerHome EJB and resource references: JNDI names list
For example, to log on a user, the Login Session EJB uses the Customer EJB to find the user in a database. The Login Session EJB looks up the ejb/Customer entry in the JNDI name space. This is then mapped to plantsby/CustomerHome, which is the JNDI name the Customer EJB is bound to.
So the EJB reference is an alias used to find the target EJB. When using EJB references, the name of the reference must be prefixed with java:comp/env. So the Login Session EJB would do a JNDI lookup with the name java:comp/env/ejb/Customer to get the EJB reference for Customer.
Follow these steps to bind an EJB to a JNDI name:
1. In the EJB Deployment Descriptor for the PlantsByWebSphereEJB module (see Figure 13-9), click the References tab.
2. Click the plus sign next to the Login Session EJB and select the ejb/Customer reference, as shown in Figure 13-10.
Figure 13-10 Setting EJB References bindings
3. In the WebSphere bindings section, specify plantsby/CustomerHome.
4. Save the deployment descriptor.
EJB reference bindings can be defined or overridden at deployment time in the console for all modules except for application clients, for which use the Application Server Toolkit.