来源:
http://java.sun.com/products/jndi/tutorial/ldap/connect/pool.htmlSample:
import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;
/**
* Demonstrates how to enable connection pooling. Use debug option
* to observe connection usage.
*
* usage: java -Dcom.sun.jndi.ldap.connect.pool.debug=fine UsePool
*/
class UsePool {
public static void main(String[] args) {
// Set up environment for creating initial context
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");
// Enable connection pooling
env.put("com.sun.jndi.ldap.connect.pool", "true");
try {
// Create one initial context (Get connection from pool)
DirContext ctx = new InitialDirContext(env);
System.out.println(ctx.getAttributes("ou=NewHires"));
// do something useful with ctx
// Close the context when we're done
ctx.close(); // Return connection to pool
// Create another initial context (Get connection from pool)
DirContext ctx2 = new InitialDirContext(env);
System.out.println(ctx2.getAttributes("ou=People"));
// do something useful with ctx2
// Close the context when we're done
ctx2.close(); // Return connection to pool
} catch (NamingException e) {
e.printStackTrace();
}
}
}
Creation Timeout
The pool of connections maintained by the LDAP service provider may be limited in size; this is described in detail in the
Connection Pooling Configuration section. When connection pooling has been enabled and no pooled connection is available, the client application will block, waiting for an available connection. You can use the
"com.sun.jndi.ldap.connect.timeout" environment property to specify how long to wait for a pooled connection. If you omit this property, the application will wait indefinitely.
This same property is also used to specify a timeout period for establishment of the LDAP connection, as described in the Connection Creation section.
When Not to Use Pooling
Pooled connections are intended to be reused. Therefore, if you plan to perform operations on a
Context instance that might alter the underlying connection's state, then you should not use connection pooling for that
Context instance. For example, if you plan to invoke the Start TLS extended operation on a
Context instance, or plan to change security-related properties (such as
"java.naming.security.principal" or
"java.naming.security.protocol") after the initial context has been created, you should not use connection pooling for that
Context instance because the LDAP provider does not track any such state changes. If you use connection pooling in such situations, you might be compromising the security of your application.
----------------------------------------------------------------------------------------------------
Connection Pooling Configuration
http://java.sun.com/products/jndi/tutorial/ldap/connect/config.html