Configuring pooling should look very familiar if you're used
to Jakarta Commons-Pool or Commons-DBCP. You will first
create a normal
ContextSource
then wrap it in a
PoolingContextSource
.
<beans> ... <bean id="contextSource" class="org.springframework.ldap.pool.factory.PoolingContextSource"> <property name="contextSource" ref="contextSourceTarget" /> </bean> <bean id="contextSourceTarget" class="org.springframework.ldap.core.support.LdapContextSource"> <property name="url" value="ldap://localhost:389" /> <property name="base" value="dc=example,dc=com" /> <property name="userDn" value="cn=Manager" /> <property name="password" value="secret" /> <property name="pooled" value="false"/> </bean> ... </beans>
In a real world example you would probably configure the pool options and enable connection validation; the above serves as an example to demonstrate the general idea.
Ensure that the
pooled
property is set to
false
on any
ContextSource
that will be wrapped in a
PoolingContextSource
. The
PoolingContextSource
must be able to create new connections when needed
and if
pooled
is set to
true
that may not be possible.
You'll notice that the actual
ContextSource
gets an id with a "Target" suffix. The bean you will
actually refer to is the
PoolingContextSource
that wraps the target
contextSource
Adding validation and a few pool configuration tweaks to
the above example is straight forward. Inject a
DirContextValidator
and set when validation should occur and the pool is
ready to go.
<beans> ... <bean id="contextSource" class="org.springframework.ldap.pool.factory.PoolingContextSource"> <property name="contextSource" ref="contextSourceTarget" /> <property name="dirContextValidator" ref="dirContextValidator" /> <property name="testOnBorrow" value="true" /> <property name="testWhileIdle" value="true" /> </bean> <bean id="dirContextValidator" class="org.springframework.ldap.pool.validation.DefaultDirContextValidator" /> <bean id="contextSourceTarget" class="org.springframework.ldap.core.support.LdapContextSource"> <property name="url" value="ldap://localhost:389" /> <property name="base" value="dc=example,dc=com" /> <property name="userDn" value="cn=Manager" /> <property name="password" value="secret" /> <property name="pooled" value="false"/> </bean> ... </beans>
The above example will test each
DirContext
before it is passed to the client application and test
DirContext
s that have been sitting idle in the pool.