ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzatz_5.4.0.1/51/program/jndicchx.htm

97 lines
2.6 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<!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 &quot;com.ibm.ejs.ns.WsnInitialContextFactory&quot; 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, &quot;60&quot;);
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(&quot;java:comp/env/jdbc/MyDataSource&quot;);
// Narrow, etc. ...
// Do not use cache if data is volatile.
o = noCacheCtx.lookup(&quot;com/mycom/VolatileObject&quot;);
// ...
}
catch(javax.naming.NamingException e) {
// error-handling code
}
</pre>
</body>
</html>