spring ldap pool 连接池

遇到一个怪怪的问题:

Java代码  收藏代码
  1. org.springframework.ldap.CommunicationException: connection closed; nested exception is javax.naming.CommunicationException: connection closed [Root exception is java.io.IOException: connection closed]; remaining name 'cn=001'  

 

到javaeye上搜了下,找不到类似问题,只是说CommunicationException是协议错误。

看到很多关于这方面的异常都是出现在证书错误方面,没有出现IOException。

当出现该异常之后,后面的连接居然都连不上了。

 

修改了一下Spring LDAP配置,去掉了pool可以了。记录下此问题,以后再慢慢补充。

Xml代码  收藏代码
  1. <!-- spring ldap source配置 -->  
  2. <bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">  
  3.     <property name="url" value="ldap://192.168.0.1:389/" />  
  4.     <property name="base" value="cn=test,DC=SCTEL,DC=COM,DC=CN" />  
  5.     <!--<property name="referral" value="follow"></property>-->  
  6.     <property name="userDn" value="cn=root" />  
  7.     <property name="password" value="123456" />  
  8. </bean>  
  9. <!--<bean id="poolingContextSource" class="org.springframework.ldap.pool.factory.PoolingContextSource">  
  10.     <property name="contextSource" ref="contextSource" />  
  11.     <property name="maxActive" value="20" />  
  12.     <property name="maxTotal" value="40" />  
  13.     <property name="maxIdle" value="10" />  
  14.     <property name="minIdle" value="5" />  
  15.     <property name="maxWait" value="5" />  
  16. </bean>-->  
  17. <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">  
  18.     <constructor-arg ref="contextSource" />  
  19.     <!--<property name="contextSource" ref="poolingContextSource" />-->  
  20. </bean>  
  21. <bean id="springLdapDao" class="com.zzt.ldap.SpringLdapDao">  
  22.     <property name="ldapTemplate" ref="ldapTemplate" />  
  23. </bean>  
  24. <bean id="ldapManager" class="com.zzt.ldap.LdapManager">  
  25.     <property name="springLdapDao" ref="springLdapDao" />  
  26. </bean>  

 

 

-----------------------------------------------------------------------------------------------------------------

补充:找出问题原因了,如下

当LDAP服务器重新启动之后,客户端无法重连导致上面问题的产生,配置修改如下就可以了

还是使用 poolingContextSource 这个bean,在该bean中添加

Xml代码  收藏代码
  1. <property name="dirContextValidator" ref="dirContextValidator" />  
  2. <property name="testOnBorrow" value="true" />  
  3. <property name="testWhileIdle" value="true" />  

 

引用的bean为:

Xml代码  收藏代码
  1. <bean id="dirContextValidator"  class="org.springframework.ldap.pool.validation.DefaultDirContextValidator" />  

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
	<!-- spring ldap source配置 -->
	<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
		<property name="url" value="ldap://127.0.0.1:389/" />
		<property name="base" value="cn=test,DC=SCTEL,DC=COM,DC=CN" />
		<property name="userDn" value="cn=root" />
		<property name="password" value="123456" />
	</bean>
	<bean id="poolingContextSource" class="org.springframework.ldap.pool.factory.PoolingContextSource">
		<property name="contextSource" ref="contextSource" />
		<property name="dirContextValidator" ref="dirContextValidator" />
		<property name="maxActive" value="20" />
		<property name="maxTotal" value="40" />
		<property name="maxIdle" value="10" />
		<property name="minIdle" value="5" />
		<property name="maxWait" value="5" />
		<property name="testOnBorrow" value="true" />
		<property name="testWhileIdle" value="true" />
	</bean>
	<bean id="dirContextValidator" class="org.springframework.ldap.pool.validation.DefaultDirContextValidator" />
	<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
		<property name="contextSource" ref="poolingContextSource" />
	</bean>
	<bean id="springLdapDao" class="com.zzt.application.common.SpringLdapDao">
		<property name="ldapTemplate" ref="ldapTemplate" />
	</bean>
	<bean id="LdapManager" class="com.zzt.application.common.LdapManager">
		<property name="springLdapDao" ref="springLdapDao" />
	</bean>
</beans>



附件给出了SpringLDAP测试代码。

包含的jar文件:

commons-lang.jar

commons-logging.jar

commons-pool-1.3.jar

spring.jar

spring-ldap-1.3.0.RELEASE-all.jar

原文地址:https://www.cnblogs.com/hzcya1995/p/13318049.html