Skip to content

Commit d77d387

Browse files
committed
HV-1088 Making sure non-default constraint validators are released when removed from the cache;
1 parent affc211 commit d77d387

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

engine/src/main/java/org/hibernate/validator/internal/engine/constraintvalidation/ConstraintValidatorManager.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
import java.lang.annotation.Annotation;
1212
import java.lang.reflect.Type;
1313
import java.util.ArrayList;
14+
import java.util.Iterator;
1415
import java.util.List;
1516
import java.util.Map;
17+
import java.util.Map.Entry;
1618
import java.util.concurrent.ConcurrentHashMap;
1719

1820
import javax.validation.ConstraintValidator;
@@ -187,15 +189,15 @@ private <V, A extends Annotation> ConstraintValidator<A, V> createAndInitializeV
187189
}
188190

189191
private void clearEntriesForFactory(ConstraintValidatorFactory constraintFactory) {
190-
List<CacheKey> entriesToRemove = new ArrayList<CacheKey>();
191-
for ( Map.Entry<CacheKey, ConstraintValidator<?, ?>> entry : constraintValidatorCache.entrySet() ) {
192-
if ( entry.getKey().getConstraintFactory() == constraintFactory ) {
193-
entriesToRemove.add( entry.getKey() );
192+
Iterator<Entry<CacheKey, ConstraintValidator<?, ?>>> cacheEntries = constraintValidatorCache.entrySet().iterator();
193+
194+
while ( cacheEntries.hasNext() ) {
195+
Entry<CacheKey, ConstraintValidator<?, ?>> cacheEntry = cacheEntries.next();
196+
if ( cacheEntry.getKey().getConstraintFactory() == constraintFactory ) {
197+
constraintFactory.releaseInstance( cacheEntry.getValue() );
198+
cacheEntries.remove();
194199
}
195200
}
196-
for ( CacheKey key : entriesToRemove ) {
197-
constraintValidatorCache.remove( key );
198-
}
199201
}
200202

201203
public void clear() {

0 commit comments

Comments
 (0)