Control JNDI cache behavior from a program

 

import java.util.Hashtable;
import javax.naming.InitialContext;
import javax.naming.Context;
import com.ibm.websphere.naming.PROPS;


/*****
*** 
*** Assume the property...
*** 
***     java.naming.factory.initial
*** 
*** ...is set to...
*** 
***     "com.ibm.websphere.naming.WsnInitialContextFactory" ...
*** 
*** ...as a java.lang.System property.
*** 
*****/

Hashtable env;
Context ctx;


// Clear a cache...

env = new Hashtable();
env.put(PROPS.JNDI_CACHE_OBJECT, PROPS.JNDI_CACHE_OBJECT_CLEARED);
ctx = new InitialContext(env);


// Set a cache's maximum cache lifetime to 60 minutes...

env = new Hashtable();
env.put(PROPS.JNDI_CACHE_MAX_LIFE, "60");
ctx = new InitialContext(env);


// Turn caching off...

env = new Hashtable();
env.put(PROPS.JNDI_CACHE_OBJECT, PROPS.JNDI_CACHE_OBJECT_NONE);
ctx = new InitialContext(env);


// Use caching and no caching...

env = new Hashtable();
env.put(PROPS.JNDI_CACHE_OBJECT, PROPS.JNDI_CACHE_OBJECT_POPULATED);
ctx = new InitialContext(env);
env.put(PROPS.JNDI_CACHE_OBJECT, PROPS.JNDI_CACHE_OBJECT_NONE);
Context noCacheCtx = new InitialContext(env);

Object o;


// Use caching to look up home, since the home should rarely change.
o = ctx.lookup("com/mycom/MyEJBHome");

// Narrow, etc. ...


// Do not use cache if data is volatile.
o = noCacheCtx.lookup("com/mycom/VolatileObject");

// ...