您的位置:首页 > 编程语言 > Java开发

spring ldap pool 连接池

2015-06-27 21:32 375 查看
遇到一个怪怪的问题:

Java代码


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代码


<!-- spring ldap source配置 -->

<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">

<property name="url" value="ldap://192.168.0.1:389/" />

<property name="base" value="cn=test,DC=SCTEL,DC=COM,DC=CN" />

<!--<property name="referral" value="follow"></property>-->

<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="maxActive" value="20" />

<property name="maxTotal" value="40" />

<property name="maxIdle" value="10" />

<property name="minIdle" value="5" />

<property name="maxWait" value="5" />

</bean>-->

<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">

<constructor-arg ref="contextSource" />

<!--<property name="contextSource" ref="poolingContextSource" />-->

</bean>

<bean id="springLdapDao" class="com.zzt.ldap.SpringLdapDao">

<property name="ldapTemplate" ref="ldapTemplate" />

</bean>

<bean id="ldapManager" class="com.zzt.ldap.LdapManager">

<property name="springLdapDao" ref="springLdapDao" />

</bean>

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

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

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

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

Xml代码


<property name="dirContextValidator" ref="dirContextValidator" />

<property name="testOnBorrow" value="true" />

<property name="testWhileIdle" value="true" />

引用的bean为:

Xml代码


<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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: