97 lines
2.6 KiB
HTML
97 lines
2.6 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
|
|
|
|
<title>JNDI coding examples</title>
|
|
</head>
|
|
|
|
<BODY>
|
|
<!-- Java sync-link -->
|
|
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
|
|
|
|
<h6><a name="jndicchx"></a>JNDI coding examples</h6>
|
|
|
|
<p>This Java code snippet demonstrates several techniques for controlling JNDI cache behavior, discussed in <a href="jndicchp.htm">JNDI cache properties</a>. Note that the caching discussed in this section pertains only to the WebSphere Application Server - Express implementation of the initial context factory. Assume that the property, java.naming.factory.initial, is set to "com.ibm.ejs.ns.WsnInitialContextFactory" as a java.lang.System property.</p>
|
|
|
|
<pre>
|
|
import java.util.Hashtable;
|
|
import javax.naming.InitialContext;
|
|
import javax.naming.Context;
|
|
import com.ibm.websphere.naming.PROPS;
|
|
|
|
// ... class declaration, method declaration code here ...
|
|
|
|
Hashtable env;
|
|
Context ctx;
|
|
|
|
// To clear a cache:
|
|
// ...class declaration, method declaration code here...
|
|
|
|
try {
|
|
env = new Hashtable();
|
|
env.put(PROPS.JNDI_CACHE_OBJECT, PROPS.JNDI_CACHE_OBJECT_CLEARED);
|
|
ctx = new InitialContext(env);
|
|
}
|
|
catch(javax.naming.NamingException e) {
|
|
// error-handling code
|
|
}
|
|
|
|
// To set a cache's maximum cache lifetime to 60 minutes:
|
|
|
|
try {
|
|
env = new Hashtable();
|
|
env.put(PROPS.JNDI_CACHE_MAX_LIFE, "60");
|
|
ctx = new InitialContext(env);
|
|
}
|
|
catch(javax.naming.NamingException e) {
|
|
// error-handling code
|
|
}
|
|
|
|
// To turn caching off:
|
|
|
|
try {
|
|
env = new Hashtable();
|
|
env.put(PROPS.JNDI_CACHE_OBJECT, PROPS.JNDI_CACHE_OBJECT_NONE);
|
|
ctx = new InitialContext(env);
|
|
}
|
|
catch(javax.naming.NamingException e) {
|
|
// error-handling code
|
|
}
|
|
|
|
// To use caching and no caching:
|
|
|
|
try {
|
|
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);
|
|
}
|
|
catch(javax.naming.NamingException e) {
|
|
// error-handling code
|
|
}
|
|
|
|
try {
|
|
Object o;
|
|
|
|
// Use caching to look up the datasource, since it should rarely change.
|
|
o = ctx.lookup("java:comp/env/jdbc/MyDataSource");
|
|
// Narrow, etc. ...
|
|
|
|
// Do not use cache if data is volatile.
|
|
o = noCacheCtx.lookup("com/mycom/VolatileObject");
|
|
// ...
|
|
}
|
|
catch(javax.naming.NamingException e) {
|
|
// error-handling code
|
|
}
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
</body>
|
|
</html>
|