Repository: spring-projects/spring-ldap
Issue: LDAP connection not closed due to DefaultDirContextValidator

`DefaultDirContextValidator` class doesn't close `NamingEnumeration` searchResults.

As a consequence, the LDAP connection associated with the `DirContext `can't be closed when requested by the pool.

It's conform to the documentation : [https://docs.oracle.com/javase/tutorial/jndi/ldap/close.html](https://docs.oracle.com/javase/tutorial/jndi/ldap/close.html)
_"If the Context instance is sharing a connection with other Context and unterminated NamingEnumeration instances, the connection will not be closed until close() has been invoked on all such Context and NamingEnumeration instances"_

`DefaultDirContextValidator` should close the `NamingEnumeration` searchResults in a finally block : 

```
NamingEnumeration<SearchResult> = null;
...
try {
	searchResults = dirContext.search(this.base, this.filter, this.searchControls);
	...
} finally {
	if (searchResults != null) {
		try {
			searchResults.close();
		} catch (Exception e) {
		}
	}
}
```




Changed files:
core/src/main/java/org/springframework/ldap/pool/validation/DefaultDirContextValidator.java
core/src/main/java/org/springframework/ldap/pool2/validation/DefaultDirContextValidator.java

diff --git a/core/src/main/java/org/springframework/ldap/pool/validation/DefaultDirContextValidator.java b/core/src/main/java/org/springframework/ldap/pool/validation/DefaultDirContextValidator.java
index 22f31f99..53db23ed 100644
--- a/core/src/main/java/org/springframework/ldap/pool/validation/DefaultDirContextValidator.java
+++ b/core/src/main/java/org/springframework/ldap/pool/validation/DefaultDirContextValidator.java
@@ -22,6 +22,7 @@ import org.springframework.ldap.pool.DirContextType;
 import org.springframework.util.Assert;
 
 import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
 import javax.naming.directory.DirContext;
 import javax.naming.directory.SearchControls;
 import javax.naming.directory.SearchResult;
@@ -165,9 +166,10 @@ public class DefaultDirContextValidator implements DirContextValidator {
     public boolean validateDirContext(DirContextType contextType, DirContext dirContext) {
         Assert.notNull(contextType, "contextType may not be null");
         Assert.notNull(dirContext, "dirContext may not be null");
-        
+
+        NamingEnumeration<SearchResult> searchResults = null;        
         try {
-            final NamingEnumeration<SearchResult> searchResults = dirContext.search(this.base, this.filter, this.searchControls);
+            searchResults = dirContext.search(this.base, this.filter, this.searchControls);
 
             if (searchResults.hasMore()) {
                 this.logger.debug("DirContext '{}' passed validation.", dirContext);
@@ -179,6 +181,14 @@ public class DefaultDirContextValidator implements DirContextValidator {
             this.logger.debug("DirContext '{}' failed validation with an exception.", dirContext, e);
             return false;
         }
+        finally {
+            if (searchResults != null) {
+               try {
+                    searchResults.close();
+                } catch (NamingException namingException) {
+                }
+            }
+        }
 
         this.logger.debug("DirContext '{}' failed validation.", dirContext);
         return false;
diff --git a/core/src/main/java/org/springframework/ldap/pool2/validation/DefaultDirContextValidator.java b/core/src/main/java/org/springframework/ldap/pool2/validation/DefaultDirContextValidator.java
index 716fb97e..8fb655c8 100644
--- a/core/src/main/java/org/springframework/ldap/pool2/validation/DefaultDirContextValidator.java
+++ b/core/src/main/java/org/springframework/ldap/pool2/validation/DefaultDirContextValidator.java
@@ -22,6 +22,7 @@ import org.springframework.ldap.pool2.DirContextType;
 import org.springframework.util.Assert;
 
 import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
 import javax.naming.directory.DirContext;
 import javax.naming.directory.SearchControls;
 import javax.naming.directory.SearchResult;
@@ -166,12 +167,13 @@ public class DefaultDirContextValidator implements DirContextValidator {
         Assert.notNull(contextType, "contextType may not be null");
         Assert.notNull(dirContext, "dirContext may not be null");
         
+        NamingEnumeration<SearchResult> searchResults = null;
         try {
-            final NamingEnumeration<SearchResult> searchResults = dirContext.search(this.base, this.filter, this.searchControls);
+            searchResults = dirContext.search(this.base, this.filter, this.searchControls);
 
             if (searchResults.hasMore()) {
                 this.logger.debug("DirContext '{}' passed validation.", dirContext);
-
+ 
                 return true;
             }
         }
@@ -179,6 +181,14 @@ public class DefaultDirContextValidator implements DirContextValidator {
             this.logger.debug("DirContext '{}' failed validation with an exception.", dirContext, e);
             return false;
         }
+        finally {
+            if (searchResults != null) {
+                try {
+                    searchResults.close();
+                } catch (NamingException namingException) {
+                }
+            }
+        }
 
         this.logger.debug("DirContext '{}' failed validation.", dirContext);
         return false;