失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > java ldap 连接池_使用Ldap连接池

java ldap 连接池_使用Ldap连接池

时间:2023-03-25 22:18:11

相关推荐

java ldap 连接池_使用Ldap连接池

来源:/products/jndi/tutorial/ldap/connect/pool.html

Sample:

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 TimeoutThe 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 PoolingPooled 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/products/jndi/tutorial/ldap/connect/config.html

如果觉得《java ldap 连接池_使用Ldap连接池》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。