Spring LDAP:由peer重置连接

Ala*_*sta 5 java spring ldap

我正在使用Spring LdapTemplate类来访问ldap.我正在使用一个ldap连接池(PoolingContextSource类)来避免在运行时始终创建连接.但是,我有时会在我的应用程序中遇到此异常:

javax.servlet.ServletException: org.springframework.ldap.CommunicationException: Connection reset; 
nested exception is javax.naming.CommunicationException: Connection reset [Root exception is java.net.SocketException: Connection reset]; 
Remaining name: 'ou=memberlist,ou=mygroups,o=mycompany.com'
Run Code Online (Sandbox Code Playgroud)

(......)

我的ldap类在以下xml中定义

<bean id="contextSource" class="com.ibm.tp4.spring.ldap.CustomPoolingContextSource">
  <property name="contextSource" ref="contextSourceTarget" />
  <property name="testWhileIdle" value="true" />
  <property name="minEvictableIdleTimeMillis" value="300000" />
  <property name="timeBetweenEvictionRunsMillis" value="10000"/>
  <property name="dirContextValidator">
    <bean class="org.springframework.ldap.pool.validation.DefaultDirContextValidator" />
  </property>
</bean>

<bean id="contextSourceTarget" class="org.springframework.ldap.core.support.LdapContextSource">
  <property name="url" value="${ldap.url}" />
  <property name="pooled" value="false" />
  <property name="anonymousReadOnly" value="true" />
</bean>

<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
  <constructor-arg ref="contextSource" />
</bean>

<bean id="myLdapResolver" class="com.ibm.tp4.model.service.user.MyLdapResolver">
  <constructor-arg ref="ldapTemplate" />
  <property name="ldapUserSearchBase" value="${ldap.user.search_base}" />
  <property name="ldapUserEmailAddressField" value="${ldap.user.email_address}" />
  <property name="ldapAttributes" value="${ldap.user.attributes}" />
</bean>
Run Code Online (Sandbox Code Playgroud)

有谁遇到过这个问题,可以提出解决方案吗?

我想过在池属性中使用testOnReturn参数而不是现在使用的连接逐出器.当我这样做时,在浏览器中运行我的Web应用程序时会收到以下警告:

WARN [org.springframework.ldap.pool.validation.DefaultDirContextValidator] - 
DirContext 'javax.naming.ldap.InitialLdapContext@d150d15' failed validation with an 
exception.javax.naming.OperationNotSupportedException: [LDAP: error code 53 - Unwilling To Perform]; 
Remaining name: ''
Run Code Online (Sandbox Code Playgroud)

不久之后,我得到了这个例外:

org.springframework.dao.DataAccessResourceFailureException: Failed to borrow DirContext from pool.; nested exception is java.util.NoSuchElementException: Could not create a validated object, cause: ValidateObject failed  
org.springframework.ldap.pool.factory.PoolingContextSource.getContext(PoolingContextSource.java:425)
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Oli*_*ver 2

看起来超时定义太低了。Oracle 有一个官方网站,可以帮助您找出问题的根源,很可能不是“Spring”,而是 Sun Ldap 连接器或您的 Ldap 服务器。很多人反对提供链接,但我根本无法复制此页面,也许您可​​以在他们的网站上尝试“原始”声明,看看它是否也会发生。它将使您更接近您的解决方案。(可能是 ldap 超时配置)

http://docs.oracle.com/javase/tutorial/jndi/newstuff/readtimeout.html

env.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.ldap.LdapCtxFactory");
env.put("com.sun.jndi.ldap.read.timeout", "1000");
env.put(Context.PROVIDER_URL, "ldap://localhost:2001");

Server s = new Server();

try {

    // start the server
    s.start();

   // Create initial context
   DirContext ctx = new InitialDirContext(env);
   System.out.println("LDAP Client: Connected to the Server");
        :
        :
} catch (NamingException e) {
   e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)