Skip to content

Commit 23991a9

Browse files
committed
Close searchResults so LDAP context can close.
Fixes #489
1 parent 06d01e5 commit 23991a9

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

core/src/main/java/org/springframework/ldap/pool/validation/DefaultDirContextValidator.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,10 @@ public void setSearchControls(SearchControls searchControls) {
165165
public boolean validateDirContext(DirContextType contextType, DirContext dirContext) {
166166
Assert.notNull(contextType, "contextType may not be null");
167167
Assert.notNull(dirContext, "dirContext may not be null");
168-
168+
169+
NamingEnumeration<SearchResult> searchResults = null;
169170
try {
170-
final NamingEnumeration<SearchResult> searchResults = dirContext.search(this.base, this.filter, this.searchControls);
171+
searchResults = dirContext.search(this.base, this.filter, this.searchControls);
171172

172173
if (searchResults.hasMore()) {
173174
this.logger.debug("DirContext '{}' passed validation.", dirContext);
@@ -179,6 +180,11 @@ public boolean validateDirContext(DirContextType contextType, DirContext dirCont
179180
this.logger.debug("DirContext '{}' failed validation with an exception.", dirContext, e);
180181
return false;
181182
}
183+
finally {
184+
if (searchResults != null) {
185+
searchResults.close();
186+
}
187+
}
182188

183189
this.logger.debug("DirContext '{}' failed validation.", dirContext);
184190
return false;

core/src/main/java/org/springframework/ldap/pool2/validation/DefaultDirContextValidator.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,9 @@ public boolean validateDirContext(DirContextType contextType, DirContext dirCont
166166
Assert.notNull(contextType, "contextType may not be null");
167167
Assert.notNull(dirContext, "dirContext may not be null");
168168

169+
NamingEnumeration<SearchResult> searchResults = null;
169170
try {
170-
final NamingEnumeration<SearchResult> searchResults = dirContext.search(this.base, this.filter, this.searchControls);
171+
searchResults = dirContext.search(this.base, this.filter, this.searchControls);
171172

172173
if (searchResults.hasMore()) {
173174
this.logger.debug("DirContext '{}' passed validation.", dirContext);
@@ -179,6 +180,11 @@ public boolean validateDirContext(DirContextType contextType, DirContext dirCont
179180
this.logger.debug("DirContext '{}' failed validation with an exception.", dirContext, e);
180181
return false;
181182
}
183+
finally {
184+
if (searchResults != null) {
185+
searchResults.close();
186+
}
187+
}
182188

183189
this.logger.debug("DirContext '{}' failed validation.", dirContext);
184190
return false;

0 commit comments

Comments
 (0)