From 370b37fbfdd9923eed5cb0a05d073d7d7789e7c7 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 10 Oct 2017 16:10:41 +0200 Subject: [PATCH] DATACMNS-901 - AbstractMappingContext publishes entity added events outside the write lock. Previously, the publication of the event that indicated a PersistentEntity having been added to it took place before the write lock over the entities had been released. We now keep the lock smaller and publish the addition event *after* the lock has been released. --- .../mapping/context/AbstractMappingContext.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java index 8092e1130..218eb0342 100644 --- a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java +++ b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java @@ -368,12 +368,13 @@ public abstract class AbstractMappingContext type = typeInformation.getType(); + E entity = null; try { write.lock(); - final E entity = createPersistentEntity(typeInformation); + entity = createPersistentEntity(typeInformation); // Eagerly cache the entity as we might have to find it during recursive lookups. persistentEntities.put(typeInformation, Optional.of(entity)); @@ -402,18 +403,18 @@ public abstract class AbstractMappingContext(this, entity)); - } - - return Optional.of(entity); - } catch (BeansException e) { throw new MappingException(e.getMessage(), e); } finally { write.unlock(); } + + // Inform listeners + if (applicationEventPublisher != null && entity != null) { + applicationEventPublisher.publishEvent(new MappingContextEvent<>(this, entity)); + } + + return Optional.of(entity); } /*