iamjerryyeung

Thursday, September 30, 2004

jndi

If I had written the code like this:Context c = newInitialContext();
DataSource ds = c.lookup("java:comp/env/jdbc/Bank");
the container would look up 'jdbc/Bank' in the EJB's local namespace. Now using a resource reference, we can say that in this EJB, the data source known to the container as BankDB is referred to locally as jdbc/Bank. In this example 'jdbc/Bank' is the 'coded name.' In other words, using resource references allows us to resolve incorrect or unknown resource names at deployment time, without modifying the code.

So when we specify a name that begins with java:comp/env, the JNDI implementation is being told to make a lookup in the part of the name/directory that is dedicated to that particular EJB.

Starting with EJB 1.1, all beans have a default JNDI context called the enterprise naming context. The default context exists in the namespace called java:comp/env (based on a URL context for the java: URL scheme) and its subdirectories. When a bean is deployed, any beans it uses are mapped into the java:comp/env/ejb directory so that the bean references can be obtained at runtime through a simple and consistent use of the JNDI ENC. This eliminates network and implementation specific use of JNDI to obtain bean references. For example, you might use a name such as "java:comp/env/jdbc/myDB" from the initial context to name the myDB database. At the root context of the namespace is a binding with the name "comp", which is reserved for component-related bindings.
In addition, the name "env" is bound to a subtree that is reserved for the component's environment-related bindings, as defined by its deployment descriptor. The J2EE specifications recommend that EJBs be placed under the "ejb" subtree. For example, an EnrollmentCartHome EJB might be named "java:comp/env/ejb/EnrollmentCartHome".

0 Comments:

Post a Comment

<< Home