diff --git a/src/main/java/org/springframework/data/mapping/PreferredConstructor.java b/src/main/java/org/springframework/data/mapping/PreferredConstructor.java index e5c1ccdb1..8cfb6e8eb 100644 --- a/src/main/java/org/springframework/data/mapping/PreferredConstructor.java +++ b/src/main/java/org/springframework/data/mapping/PreferredConstructor.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 the original author or authors. + * Copyright 2011-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,8 @@ 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 org.springframework.beans.factory.annotation.Value; import org.springframework.data.annotation.PersistenceConstructor; @@ -36,6 +38,7 @@ import org.springframework.util.StringUtils; * * @author Oliver Gierke * @author Jon Brisbin + * @author Thomas Darimont */ public class PreferredConstructor> { @@ -43,6 +46,10 @@ public class PreferredConstructor> { private final List> parameters; private final Map, Boolean> isPropertyParameterCache = new HashMap, Boolean>(); + private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + private final Lock read = lock.readLock(); + private final Lock write = lock.writeLock(); + /** * Creates a new {@link PreferredConstructor} from the given {@link Constructor} and {@link Parameter}s. * @@ -117,21 +124,36 @@ public class PreferredConstructor> { Assert.notNull(property); - Boolean cached = isPropertyParameterCache.get(property); + try { - if (cached != null) { - return cached; - } + read.lock(); + Boolean cached = isPropertyParameterCache.get(property); - for (Parameter parameter : parameters) { - if (parameter.maps(property)) { - isPropertyParameterCache.put(property, true); - return true; + if (cached != null) { + return cached; } + + } finally { + read.unlock(); } - isPropertyParameterCache.put(property, false); - return false; + try { + + write.lock(); + + for (Parameter parameter : parameters) { + if (parameter.maps(property)) { + isPropertyParameterCache.put(property, true); + return true; + } + } + + isPropertyParameterCache.put(property, false); + return false; + + } finally { + write.unlock(); + } } /**