Home TOC |
Search
Feedback |
JNDI Names and Resource References
Definitions
A JNDI name is a people-friendly name for an object. These names are bound to their objects by the naming and directory service that is provided by the J2EE server. Because J2EE components access this service through the JNDI API, we usually refer to an object's people-friendly name as its JNDI name.
A resource reference is an element in a deployment descriptor that identifies the component's coded name for the resource. More specifically, the coded name identifies a connection factory for the resource.
Although both the coded and the JNDI name identify the same connection factory, they are different. This approach to naming requires that you map the two names before deployment, but it also decouples components from resources. Becuase of this decoupling, if at a later time the component needs to access a different resource, you don't have to change the name in the code. This flexibility also makes it easier for you to assemble J2EE applications from pre-existing components.
Specifying a Resource Reference
The instructions that follow mention the AccountEJB example, which is described in the section, A Bean-Managed Persistence Example. The AccountEJB code is in the examples/src/ejb/account directory.
- In the deploytool, select the component from the tree.
- Select the Resource Ref's tab.
- Click Add.
- In the Coded Name field, enter jdbc/AccountDB.
- The AccountEJB refers to the database as follows:
private String dbName = "java:comp/env/jdbc/AccountDB";
- The java:comp/env prefix is the JNDI subcontext for the component. Becuase this subcontext is implicit in the Coded Name field, you don't need to include it there.
- In the Type combo box, select javax.sql.DataSource. A DataSource object is a factory for database connections.
- In the Authentication combo box, select Container.
Mapping a Resource Reference to a JNDI Name
- Select the J2EE application from the tree.
- Select the JNDI Names tab.
- In the References table, select the row containing the resource reference. For the AccountEJB example, the resource reference is jdbc/AccountDB, the name you entered in the Coded Name field of the Resource Ref's tab.
- In the row you just selected, enter the JNDI name. For the AccountEJB example, you would enter jdbc/Cloudscape in the JNDI Name field.
Home TOC |
Search
Feedback |