title: Spring LDAP: Connection reset by peer
body: <p>I'm using Spring LdapTemplate class to access ldap. I'm using a pool of ldap connections (PoolingContextSource class) to avoid creating connections all the time at runtime. However, I get this exception sometimes at my application:</p>
  
  <pre><code>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'
  </code></pre>
  
  <p>(...)</p>
  
  <p>My ldap classes are defined in the following xml</p>
  
  <pre><code>&lt;bean id="contextSource" class="com.ibm.tp4.spring.ldap.CustomPoolingContextSource"&gt;
    &lt;property name="contextSource" ref="contextSourceTarget" /&gt;
    &lt;property name="testWhileIdle" value="true" /&gt;
    &lt;property name="minEvictableIdleTimeMillis" value="300000" /&gt;
    &lt;property name="timeBetweenEvictionRunsMillis" value="10000"/&gt;
    &lt;property name="dirContextValidator"&gt;
      &lt;bean class="org.springframework.ldap.pool.validation.DefaultDirContextValidator" /&gt;
    &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="contextSourceTarget" class="org.springframework.ldap.core.support.LdapContextSource"&gt;
    &lt;property name="url" value="${ldap.url}" /&gt;
    &lt;property name="pooled" value="false" /&gt;
    &lt;property name="anonymousReadOnly" value="true" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate"&gt;
    &lt;constructor-arg ref="contextSource" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="myLdapResolver" class="com.ibm.tp4.model.service.user.MyLdapResolver"&gt;
    &lt;constructor-arg ref="ldapTemplate" /&gt;
    &lt;property name="ldapUserSearchBase" value="${ldap.user.search_base}" /&gt;
    &lt;property name="ldapUserEmailAddressField" value="${ldap.user.email_address}" /&gt;
    &lt;property name="ldapAttributes" value="${ldap.user.attributes}" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Has anyone experienced this problem and can suggest a solution? </p>
  
  <p>I thought about using testOnReturn parameter in the pool properties instead of connection evictor used right now. When I do, I get the following warning when I run my web application in the browser:</p>
  
  <pre><code>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: ''
  </code></pre>
  
  <p>and soon after, I get this exception: </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Thanks in advance.</p>