This version is still in development and is not considered stable yet. For the latest stable version, please use Spring LDAP 3.2.8!spring-doc.cn

Pooling Support

Pooling LDAP connections helps mitigate the overhead of creating a new LDAP connection for each LDAP interaction. While Java LDAP pooling support exists, it is limited in its configuration options and features, such as connection validation and pool maintenance. Spring LDAP provides support for detailed pool configuration on a per-ContextSource basis.spring-doc.cn

Pooling support is provided by supplying a <ldap:pooling /> child element to the <ldap:context-source /> element in the application context configuration. Read-only and read-write DirContext objects are pooled separately (if anonymous-read-only is specified). Jakarta Commons-Pool is used to provide the underlying pool implementation.spring-doc.cn

DirContext Validation

Validation of pooled connections is the primary motivation for using a custom pooling library versus the JDK-provided LDAP pooling functionality. Validation allows pooled DirContext connections to be checked to ensure that they are still properly connected and configured when checking them out of the pool, checking them into the pool, or while they are idle in the pool.spring-doc.cn

If connection validation is configured, pooled connections are validated by using DefaultDirContextValidator. DefaultDirContextValidator does a DirContext.search(String, String, SearchControls), with an empty name, a filter of "objectclass=*", and SearchControls set to limit a single result with the only the objectclass attribute and a 500ms timeout. If the returned NamingEnumeration has results, the DirContext passes validation. If no results are returned or an exception is thrown, the DirContext fails validation. The default settings should work with no configuration changes on most LDAP servers and provide the fastest way to validate the DirContext. If you need customization, you can do so by using the validation configuration attributes, described in Pool Configuration.spring-doc.cn

Connections are automatically invalidated if they throw an exception that is considered non-transient. For example, if a DirContext instance throws a javax.naming.CommunicationException, it is interpreted as a non-transient error and the instance is automatically invalidated, without the overhead of an additional testOnReturn operation. The exceptions that are interpreted as non-transient are configured by using the nonTransientExceptions property of the PoolingContextSource.

Pool Configuration

The following attributes are available on the <ldap:pooling /> element for configuration of the DirContext pool:spring-doc.cn

Table 1. Pooling Configuration Attributes
Attribute Default Description

max-activespring-doc.cn

8spring-doc.cn

The maximum number of active connections of each type (read-only or read-write) that can be allocated from this pool at the same time. You can use a non-positive number for no limit.spring-doc.cn

max-totalspring-doc.cn

-1spring-doc.cn

The overall maximum number of active connections (for all types) that can be allocated from this pool at the same time. You can use a non-positive number for no limit.spring-doc.cn

max-idlespring-doc.cn

8spring-doc.cn

The maximum number of active connections of each type (read-only or read-write) that can remain idle in the pool without extra connections being released. You can use a non-positive number for no limit.spring-doc.cn

min-idlespring-doc.cn

0spring-doc.cn

The minimum number of active connections of each type (read-only or read-write) that can remain idle in the pool without extra connections being created. You can use zero (the default) to create none.spring-doc.cn

max-waitspring-doc.cn

-1spring-doc.cn

The maximum number of milliseconds that the pool waits (when no connections are available) for a connection to be returned before throwing an exception. You can use a non-positive number to wait indefinitely.spring-doc.cn

when-exhaustedspring-doc.cn

BLOCKspring-doc.cn

Specifies the behavior when the pool is exhausted.spring-doc.cn

  • The FAIL option throws NoSuchElementException when the pool is exhausted.spring-doc.cn

  • The BLOCK option waits until a new object is available. If max-wait is positive and no new object is available after the max-wait time expires, NoSuchElementException is thrown.spring-doc.cn

  • The GROW option creates and returns a new object (essentially making max-active meaningless).spring-doc.cn

test-on-borrowspring-doc.cn

falsespring-doc.cn

Whether objects are validated before being borrowed from the pool. If the object fails to validate, it is dropped from the pool, and an attempt to borrow another is made.spring-doc.cn

test-on-returnspring-doc.cn

falsespring-doc.cn

Whether objects are validated before being returned to the pool.spring-doc.cn

test-while-idlespring-doc.cn

falsespring-doc.cn

Whether objects are validated by the idle object evictor (if any). If an object fails to validate, it is dropped from the pool.spring-doc.cn

eviction-run-interval-millisspring-doc.cn

-1spring-doc.cn

The number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread is run.spring-doc.cn

tests-per-eviction-runspring-doc.cn

3spring-doc.cn

The number of objects to examine during each run of the idle object evictor thread (if any).spring-doc.cn

min-evictable-time-millisspring-doc.cn

1000 * 60 * 30 (30 minutes)spring-doc.cn

The minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any).spring-doc.cn

validation-query-basespring-doc.cn

LdapUtils.emptyName()spring-doc.cn

The search base to be used when validating connections. Used only if test-on-borrow, test-on-return, or test-while-idle is specified.spring-doc.cn

validation-query-filterspring-doc.cn

objectclass=*spring-doc.cn

The search filter to be used when validating connections. Used only if test-on-borrow, test-on-return, or test-while-idle is specified.spring-doc.cn

validation-query-search-controls-refspring-doc.cn

null; default search control settings are described above.spring-doc.cn

The ID of a SearchControls instance to be used when validating connections. Only used if test-on-borrow, test-on-return, or test-while-idle is specified.spring-doc.cn

non-transient-exceptionsspring-doc.cn

javax.naming.CommunicationExceptionspring-doc.cn

Comma-separated list of Exception classes. The listed exceptions are considered non-transient with regards to eager invalidation. Should any of the listed exceptions (or subclasses of them) be thrown by a call to a pooled DirContext instance, that object is automatically invalidated without any additional testOnReturn operation.spring-doc.cn

Pool2 Configuration

The following attributes are available on the <ldap:pooling2 /> element for configuring the DirContext pool:spring-doc.cn

Table 2. Pooling Configuration Attributes
Attribute Default Description

max-totalspring-doc.cn

-1spring-doc.cn

The overall maximum number of active connections (for all types) that can be allocated from this pool at the same time. You can use a non-positive number for no limit.spring-doc.cn

max-total-per-keyspring-doc.cn

8spring-doc.cn

The limit on the number of object instances allocated by the pool (checked out or idle), per key. When the limit is reached, the sub-pool is exhausted. A negative value indicates no limit.spring-doc.cn

max-idle-per-keyspring-doc.cn

8spring-doc.cn

The maximum number of active connections of each type (read-only or read-write) that can remain idle in the pool, without extra connections being released. A negative value indicates no limit.spring-doc.cn

min-idle-per-keyspring-doc.cn

0spring-doc.cn

The minimum number of active connections of each type (read-only or read-write) that can remain idle in the pool, without extra connections being created. You can use zero (the default) to create none.spring-doc.cn

max-waitspring-doc.cn

-1spring-doc.cn

The maximum number of milliseconds that the pool waits (when there are no available connections) for a connection to be returned before throwing an exception. You can use a non-positive number to wait indefinitely.spring-doc.cn

block-when-exhaustedspring-doc.cn

truespring-doc.cn

Whether to wait until a new object is available. If max-wait is positive, a NoSuchElementException is thrown if no new object is available after the maxWait time expires.spring-doc.cn

test-on-createspring-doc.cn

falsespring-doc.cn

Whether objects are validated before borrowing. If the object fails to validate, then borrowing fails.spring-doc.cn

test-on-borrowspring-doc.cn

falsespring-doc.cn

The indicator for whether objects are validated before being borrowed from the pool. If the object fails to validate, it is dropped from the pool, and an attempt to borrow another is made.spring-doc.cn

test-on-returnspring-doc.cn

falsespring-doc.cn

The indicator for whether objects are validated before being returned to the pool.spring-doc.cn

test-while-idlespring-doc.cn

falsespring-doc.cn

The indicator for whether objects are validated by the idle object evictor (if any). If an object fails to validate, it is dropped from the pool.spring-doc.cn

eviction-run-interval-millisspring-doc.cn

-1spring-doc.cn

The number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread is run.spring-doc.cn

tests-per-eviction-runspring-doc.cn

3spring-doc.cn

The number of objects to examine during each run of the idle object evictor thread (if any).spring-doc.cn

min-evictable-time-millisspring-doc.cn

1000 * 60 * 30 (30 minutes)spring-doc.cn

The minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any).spring-doc.cn

soft-min-evictable-time-millisspring-doc.cn

-1spring-doc.cn

The minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor, with the extra condition that at least the minimum number of object instances per key remain in the pool. This setting is overridden by min-evictable-time-millis if it is set to a positive value.spring-doc.cn

eviction-policy-classspring-doc.cn

org.apache.commons.pool2.impl.DefaultEvictionPolicyspring-doc.cn

The eviction policy implementation that is used by this pool. The pool tries to load the class by using the thread context class loader. If that fails, the pool tries to load the class by using the class loader that loaded this class.spring-doc.cn

fairnessspring-doc.cn

falsespring-doc.cn

The pool serves threads that are waiting to borrow connections fairly. true means that waiting threads are served as if waiting in a FIFO queue.spring-doc.cn

jmx-enablespring-doc.cn

truespring-doc.cn

JMX is enabled with the platform MBean server for the pool.spring-doc.cn

jmx-name-basespring-doc.cn

nullspring-doc.cn

The JMX name base that is used as part of the name assigned to JMX enabled pools.spring-doc.cn

jmx-name-prefixspring-doc.cn

poolspring-doc.cn

The JMX name prefix that is used as part of the name assigned to JMX enabled pools.spring-doc.cn

lifospring-doc.cn

truespring-doc.cn

The indicator for whether the pool has LIFO (last in, first out) behavior with respect to idle objects or as a FIFO (first in, first out) queue. LIFO always returns the most recently used object from the pool, while FIFO always returns the oldest object in the idle object poolspring-doc.cn

validation-query-basespring-doc.cn

LdapUtils.emptyPath()spring-doc.cn

The base DN to use for validation searches.spring-doc.cn

validation-query-filterspring-doc.cn

objectclass=*spring-doc.cn

The filter to use for validation queries.spring-doc.cn

validation-query-search-controls-refspring-doc.cn

null; default search control settings are described above.spring-doc.cn

The ID of a SearchControls instance to be used when validating connections. Used only if test-on-borrow, test-on-return, or test-while-idle is specifiedspring-doc.cn

non-transient-exceptionsspring-doc.cn

javax.naming.CommunicationExceptionspring-doc.cn

Comma-separated list of Exception classes. The listed exceptions are considered non-transient with regards to eager invalidation. Should any of the listed exceptions (or subclasses of them) be thrown by a call to a pooled DirContext instance, that object is automatically invalidated without any additional testOnReturn operation.spring-doc.cn

Configuration

Configuring pooling requires adding an <ldap:pooling> element nested in the <ldap:context-source> element, as follows:spring-doc.cn

<beans>
   ...
    <ldap:context-source
        password="secret" url="ldap://localhost:389" username="cn=Manager">
        <ldap:pooling />
    </ldap:context-source>
   ...
</beans>

In a real-world situation, you would probably configure the pool options and enable connection validation. The preceding example demonstrates the general idea.spring-doc.cn

Validation Configuration

The following example tests each DirContext before it is passed to the client application and tests DirContext objects that have been sitting idle in the pool:spring-doc.cn

<beans>
   ...
    <ldap:context-source
        username="cn=Manager" password="secret" url="ldap://localhost:389" >
        <ldap:pooling
            test-on-borrow="true"
            test-while-idle="true" />
    </ldap:context-source>
   ...
</beans>

Known Issues

This section describes issues that sometimes arise when people use Spring LDAP. At present, it covers the following issues:spring-doc.cn

Custom Authentication

The PoolingContextSource assumes that all DirContext objects retrieved from ContextSource.getReadOnlyContext() have the same environment and, likewise, that all DirContext objects retrieved from ContextSource.getReadWriteContext() have the same environment. This means that wrapping an LdapContextSource configured with an AuthenticationSource in a PoolingContextSource does not function as expected. The pool would be populated by using the credentials of the first user, and, unless new connections were needed, subsequent context requests would not be filled for the user specified by the AuthenticationSource for the requesting thread.spring-doc.cn