2012-09-17 112 views
4

我正在使用Spring LdapTemplate类访问ldap。我正在使用一个ldap连接池(PoolingContextSource类)来避免在运行时一直创建连接。不过,我有时会得到这个例外在我的应用程序:春天LDAP:连接重置由对端

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' 

(...)

我的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> 

有没有人遇到过这个问题,可以建议解决方案?

我想过在池属性中使用testOnReturn参数,而不是现在使用的连接排除器。当我这样做,我得到以下警告当我运行在浏览器中我的web应用程序:

WARN [org.springframework.ldap.pool.validation.DefaultDirContextValidator] - 
DirContext '[email protected]' failed validation with an 
exception.javax.naming.OperationNotSupportedException: [LDAP: error code 53 - Unwilling To Perform]; 
Remaining name: '' 

不久之后,我得到这个异常:

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) 

在此先感谢。

回答

2

它看起来像超时定义是低的方式。有一个来自Oracle的官方网站,可以帮助您找出问题的根源,很可能它不是Sun的Ldap连接器或Ldap服务器的“Spring”。很多人反对提供链接,但我不能复制此页面,也许你会在他们的网站上尝试“原始”声明以查看它是否也发生了。它会让您更接近您的解决方案。 (可能是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(); 
} 
+0

1)我使用了Spring LDAP,它是围绕Java(JNDI)LDAP的包装。我不知道如何使用Spring库设置这个超时。 2)我添加了一个警告,显示在我的问题的异常之前,问题似乎是一个无效的操作,而不是超时。 –

+0

你想要执行什么操作? –

+0

我只是设置了Sprint LDAP属性,用于测试在使用LDAP连接池时从LDAP连接池获得的每个LDAP连接是否有效。可能Spring会向LDAP服务器发送一个简单的虚拟请求来验证连接,但我不知道该请求执行的是哪个ldap操作。我不得不阅读框架代码来弄清楚。 –