JNDI Caching

 


Overview

The WAS JNDI implementation uses caching to reduce the number of remote calls to the name server.

InitialContext instances and JNDI caches are associated based on the provider URL's host name and port. The caller can specify the cache name to override this default behavior.

Two EJB applications running in the same server will use their own cache instances, if they are using different context class loaders, even if the cache names are the same.

After an association between an InitialContext instance and cache is established, the association does not change. A javax.naming.Context object returned from a lookup operation inherits the cache association of the Context object on which the lookup was performed.

Changing cache property values with java.lang.String, java.lang.Object)">addToEnvironment() or java.lang.String)">removeFromEnvironment() does not affect cache behavior. You can change properties affecting a given cache instance with each InitialContext instantiation.

A cache is restricted to a process and does not persist past the life of that process. A cached object is returned from lookup operations until either the max cache life for the cache is reached, or the max entry life for the object's cache entry is reached.

After this time, a lookup on the object causes the cache entry for the object to be refreshed. If a bind or rebind operation is executed on an object, the change is not reflected in any caches other than the one associated with the context from which the bind or rebind was issued. This scenario is most likely to happen when multiple processes are involved, since different processes do not share the same cache, and context objects in all threads in a process typically share the same cache instance for a given name service provider.

Usually, cached objects are relatively static entities, and objects becoming stale are not a problem. However, you can set timeout values on cache entries or on a cache so that cache contents are periodically refreshed.

 


Developing applications that use JNDI

References to artifacts such EJB homes and data sources are bound to the WebSphere name space and can be derived through the JNDI interface by using an initial context. The following examples describe how to obtain an initial context and perform lookups.

WebSphere's JNDI Context implementation is used in the examples, specifically JNDI caching and a choice of a name syntaxes, one optimized for typical JNDI clients, and one optimized for interoperability with CosNaming applications.

JNDI caching and name syntax options are associated with a javax.naming.InitialContext instance. To select options for these features, set properties that are recognized by the WebSphere's initial context factory.

To set JNDI caching or name syntax properties which will be visible to WAS's initial context factory, follow the following steps.

 

Configure JNDI caches

Objects are cached locally as they are looked up. Subsequent lookups on cached objects are resolved locally. Cache contents can become stale. There are several techniques one can use to update the JNDI cache:

 

Specify the name syntax

Most WebSphere applications use JNDI to look up EJB objects and do not need to look up objects bound by CORBA applications. Therefore, the default name syntax used for JNDI names is the most convenient. If your application needs to look up objects bound by CORBA applications, you may need to change your name syntax so that all CORBA CosNaming names can be represented.

JNDI clients can set the name syntax by setting a property. The property setting is applied by the initial context factory when you instantiate a new java.naming.InitialContext object. Names specified in JNDI operations on the initial context are parsed according to the specified name syntax.

Set the property...