From 1976878bd5aa781365d57b0b4ee24852792adc7a Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 26 Aug 2013 18:29:04 +0200 Subject: [PATCH] DATACMNS-362 - Resolved concurrency problem in Repositories. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now create a copy of the keys before iterating over them in Repositories.getRepoInforFor(…) to make sure other threads can safely trigger lookupRepositoryFactoryInformationFor(…) which potentially alters the repositories instance variable. So far we haven't guarded against this scenario which caused a ConcurrentModificationException. --- .../data/repository/support/Repositories.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/repository/support/Repositories.java b/src/main/java/org/springframework/data/repository/support/Repositories.java index e752b6f04..daa56e60e 100644 --- a/src/main/java/org/springframework/data/repository/support/Repositories.java +++ b/src/main/java/org/springframework/data/repository/support/Repositories.java @@ -173,7 +173,10 @@ public class Repositories implements Iterable> { Assert.notNull(domainClass); - for (RepositoryFactoryInformation information : repositories.keySet()) { + // Create defensive copy of the keys to allow threads to potentially add values while iterating over them + Set> keys = Collections.unmodifiableSet(repositories.keySet()); + + for (RepositoryFactoryInformation information : keys) { if (domainClass.equals(information.getEntityInformation().getJavaType())) { return information; }