diff --git a/src/main/java/org/springframework/data/mapping/PreferredConstructor.java b/src/main/java/org/springframework/data/mapping/PreferredConstructor.java index c6478f53cb..c3b54d7dd0 100644 --- a/src/main/java/org/springframework/data/mapping/PreferredConstructor.java +++ b/src/main/java/org/springframework/data/mapping/PreferredConstructor.java @@ -20,11 +20,9 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.util.Arrays; -import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.concurrent.ConcurrentHashMap; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.annotation.PersistenceConstructor; @@ -43,16 +41,13 @@ * @author Thomas Darimont * @author Christoph Strobl * @author Mark Paluch + * @author Myeonghyeon Lee */ public class PreferredConstructor> { private final Constructor constructor; private final List> parameters; - private final Map, Boolean> isPropertyParameterCache = new HashMap<>(); - - private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - private final Lock read = lock.readLock(); - private final Lock write = lock.writeLock(); + private final Map, Boolean> isPropertyParameterCache = new ConcurrentHashMap<>(); /** * Creates a new {@link PreferredConstructor} from the given {@link Constructor} and {@link Parameter}s. @@ -129,36 +124,22 @@ public boolean isConstructorParameter(PersistentProperty property) { Assert.notNull(property, "Property must not be null!"); - try { - - read.lock(); - Boolean cached = isPropertyParameterCache.get(property); - - if (cached != null) { - return cached; - } + Boolean cached = isPropertyParameterCache.get(property); - } finally { - read.unlock(); + if (cached != null) { + return cached; } - try { - - write.lock(); - - for (Parameter parameter : parameters) { - if (parameter.maps(property)) { - isPropertyParameterCache.put(property, true); - return true; - } + boolean result = false; + for (Parameter parameter : parameters) { + if (parameter.maps(property)) { + isPropertyParameterCache.put(property, true); + result = true; + break; } - - isPropertyParameterCache.put(property, false); - return false; - - } finally { - write.unlock(); } + + return result; } /**