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

来源:http://java.sun.com/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 Configurationhttp://java.sun.com/products/jndi/tutorial/ldap/connect/config.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值