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.
- Get the initial context
- Get an initial context by setting the provider URL
- Set the provider URL to select a different root context
- Look up an EJB home with JNDI
- Look up a JavaMail session with JNDI
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:
- Enter a string value from the commandline. For example
java -Dcom.ibm.websphere.naming.jndicache.maxentrylife=1440- In a jndi.properties file. For example
... com.ibm.websphere.naming.jndicache.cacheobject=none ...Include the file as the beginning of the classpath, so that the class loader loads your copy of jndi.properties before any other copies.
- Within a Java program by using PROPS.JNDI_CACHE* constants, defined in the com.ibm.websphere.naming.PROPS file.
public static final String JNDI_CACHE_OBJECT = "com.ibm.websphere.naming.jndicache.cacheobject";
public static final String JNDI_CACHE_OBJECT_NONE = "none";
public static final String JNDI_CACHE_OBJECT_POPULATED = "populated";
public static final String JNDI_CACHE_OBJECT_CLEARED = "cleared";
public static final String JNDI_CACHE_OBJECT_DEFAULT = JNDI_CACHE_OBJECT_POPULATED;
public static final String JNDI_CACHE_NAME = "com.ibm.websphere.naming.jndicache.cachename";
public static final String JNDI_CACHE_NAME_DEFAULT = "providerURL";
public static final String JNDI_CACHE_MAX_LIFE = "com.ibm.websphere.naming.jndicache.maxcachelife";
public static final int JNDI_CACHE_MAX_LIFE_DEFAULT = 0;
public static final String JNDI_CACHE_MAX_ENTRY_LIFE = "com.ibm.websphere.naming.jndicache.maxentrylife";
public static final int JNDI_CACHE_MAX_ENTRY_LIFE_DEFAULT = 0;To use the previous properties in a Java program, add the property setting to a hashtable and pass it to the InitialContext constructor as follows
java.util.Hashtable env = new java.util.Hashtable(); ... // Disable caching env.put(PROPS.JNDI_CACHE_OBJECT, PROPS.JNDI_CACHE_OBJECT_NONE); ... javax.naming.Context initialContext = new javax.naming.InitialContext(env);
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...
- From the command line by entering the actual string value. For example
java -Dcom.ibm.websphere.naming.name.syntax=ins- In a jndi.properties file by creating a file named jndi.properties as a text file with the desired properties settings. For example
... com.ibm.websphere.naming.name.syntax=ins ...Include the file as the beginning of the classpath, so that the class loader loads your copy of jndi.properties before any other copies.
- Within a Java program by using the PROPS.NAME_SYNTAX* Java constants, defined in the com.ibm.websphere.naming.PROPS file. The constant definitions follow
public static final String NAME_SYNTAX = "com.ibm.websphere.naming.name.syntax"; public static final String NAME_SYNTAX_JNDI = "jndi"; public static final String NAME_SYNTAX_INS = "ins";To use the previous properties in a Java program, add the property setting to a hashtable and pass it to the InitialContext constructor as follows
java.util.Hashtable env = new java.util.Hashtable(); ... env.put(PROPS.NAME_SYNTAX, PROPS.NAME_SYNTAX_INS); // Set name syntax to INS ... javax.naming.Context initialContext = new javax.naming.InitialContext(env);