Skip to content

Commit ccd8e2a

Browse files
committed
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.
1 parent 47c2a40 commit ccd8e2a

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

Diff for: src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,13 @@ protected Optional<E> addPersistentEntity(TypeInformation<?> typeInformation) {
368368
}
369369

370370
Class<?> type = typeInformation.getType();
371+
E entity = null;
371372

372373
try {
373374

374375
write.lock();
375376

376-
final E entity = createPersistentEntity(typeInformation);
377+
entity = createPersistentEntity(typeInformation);
377378

378379
// Eagerly cache the entity as we might have to find it during recursive lookups.
379380
persistentEntities.put(typeInformation, Optional.of(entity));
@@ -402,18 +403,18 @@ protected Optional<E> addPersistentEntity(TypeInformation<?> typeInformation) {
402403
throw e;
403404
}
404405

405-
// Inform listeners
406-
if (null != applicationEventPublisher) {
407-
applicationEventPublisher.publishEvent(new MappingContextEvent<>(this, entity));
408-
}
409-
410-
return Optional.of(entity);
411-
412406
} catch (BeansException e) {
413407
throw new MappingException(e.getMessage(), e);
414408
} finally {
415409
write.unlock();
416410
}
411+
412+
// Inform listeners
413+
if (applicationEventPublisher != null && entity != null) {
414+
applicationEventPublisher.publishEvent(new MappingContextEvent<>(this, entity));
415+
}
416+
417+
return Optional.of(entity);
417418
}
418419

419420
/*

0 commit comments

Comments
 (0)