diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java b/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java index c1c0ca7b3..94c1fb602 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java @@ -1648,14 +1648,21 @@ public class MetaDataRepository implements PCRegistry.RegisterClassListener, Con * before other threads attempt to call this method */ synchronized Class[] processRegisteredClasses(ClassLoader envLoader) { - if (_registered.isEmpty()) { - return EMPTY_CLASSES; - } - // copy into new collection to avoid concurrent mod errors on reentrant - // registrations - Class[] reg = _registered.toArray(new Class[_registered.size()]); - _registered.clear(); + Class[] reg; + /*Synchronize `_registered` cache to block MetaDataRepository.register() from adding + * to the cache while we copy, causing a ConcurrentModificationException + */ + synchronized (_registered) { + if (_registered.isEmpty()) { + return EMPTY_CLASSES; + } + + // copy into new collection to avoid concurrent mod errors on reentrant + // registrations + reg = _registered.toArray(new Class[_registered.size()]); + _registered.clear(); + } Collection pcNames = getPersistentTypeNames(false, envLoader); Collection> failed = null;