|
Home TOC Index |
|
Search
Feedback |
JNDI Names and Resource References
First, let's define some terms.
JNDI is the acronym for the Java Naming and Directory Interface API. J2EE components locate objects by invoking the JNDI
lookupmethod.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. The JNDI name of the Cloudscape database is
jdbc/Cloudscape. When it starts up, the J2EE server reads information from a configuration file and automatically adds JNDI database names such asjdbc/Cloudscapeto the name space.A connection factory is an object that produces connection objects that enable a J2EE component to access a resource. The connection factory for a database is a j
avax.sql.DataSourceobject, which creates ajava.sql.Connectionobject.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 references a connection factory for the resource. In the example in the following section, the resource reference name is
jdbc/SavingsAccountDB.The JNDI name of a resource and the name of the resource reference are not the same. This approach to naming requires that you map the two names before deployment, but it also decouples components from resources. Because 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 preexisting components.
deploytool Tips for Resource References
The instructions that follow refer to the entity bean described in the section The SavingsAccountEJB Example. The
SavingsAccountEJBcode is in thej2eetutorial/examples/src/ejb/savingsaccountdirectory. A sampleSavingsAccountApp.earfile is in the j2eetutorial/examples/earsdirectory.Specifying a Resource Reference
- In
deploytool, selectSavingsAccountEJBfrom the tree.- Select the Resource Refs tab.
- Click Add.
- In the Coded Name field, enter
jdbc/SavingsAccountDB.
- The
SavingsAccountBeancode refers to the database as follows:private String dbName = "java:comp/env/jdbc/SavingsAccountDB";- The
java:comp/envprefix is the name of the JNDI context for the component. Thejdbc/SavingsAccountDBstring is the JNDI name for the resource reference. The JNDI names for JDBCDataSourceobjects are stored in thejava:comp/env/jdbcsubcontext.- In the Type combo box, select
javax.sql.DataSource. ADataSourceobject is a factory for database connections.- In the Authentication combo box, select Container.
- If you want other enterprise beans to share the connections acquired from the
DataSource, select the Sharable checkbox.If the preceding steps are followed, the Resource Refs tab will appear as shown in Figure 16-1.
Figure 16-1 Resource Refs Tabbed Pane of
SavingsAccountEJBMapping 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
SavingsAccountEJBexample, the resource reference isjdbc/SavingsAccountDB, the name you entered in the Coded Name field of the Resource Refs tab.- In the row you just selected, enter the JNDI name. For the
SavingsAccountEJBexample, you would enterjdbc/Cloudscapein the JNDI Name field.
- The JNDI Names tab for
SavingsAccountAppis shown in Figure 16-2.
|
Home TOC Index |
|
Search
Feedback |