From 0538b97d6a087f77e2ca51248ef3317c6f0973a8 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Sat, 17 Aug 2019 15:58:36 +0100 Subject: [PATCH] HHH-13587 Allocate StatefulPersistenceContext#naturalIdXrefDelegate lazily --- .../internal/StatefulPersistenceContext.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java index b8cde15c70..0a681cf470 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java @@ -262,7 +262,7 @@ public class StatefulPersistenceContext implements PersistenceContext { if ( loadContexts != null ) { loadContexts.cleanup(); } - naturalIdXrefDelegate.clear(); + naturalIdXrefDelegate = null; } @Override @@ -1762,11 +1762,16 @@ public class StatefulPersistenceContext implements PersistenceContext { } } - - // NATURAL ID RESOLUTION HANDLING ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - private final NaturalIdXrefDelegate naturalIdXrefDelegate = new NaturalIdXrefDelegate( this ); + private NaturalIdXrefDelegate naturalIdXrefDelegate; + + private NaturalIdXrefDelegate getNaturalIdXrefDelegate() { + if ( naturalIdXrefDelegate == null ) { + this.naturalIdXrefDelegate = new NaturalIdXrefDelegate( this ); + } + return naturalIdXrefDelegate; + } private final NaturalIdHelper naturalIdHelper = new NaturalIdHelper() { @Override @@ -1785,7 +1790,7 @@ public class StatefulPersistenceContext implements PersistenceContext { // from a single load event. The first put journal would come from the natural id resolution; // the second comes from the entity loading. In this condition, we want to avoid the multiple // 'put' stats incrementing. - final boolean justAddedLocally = naturalIdXrefDelegate.cacheNaturalIdCrossReference( persister, id, naturalIdValues ); + final boolean justAddedLocally = getNaturalIdXrefDelegate().cacheNaturalIdCrossReference( persister, id, naturalIdValues ); if ( justAddedLocally && persister.hasNaturalIdCache() ) { managedSharedCacheEntries( persister, id, naturalIdValues, null, CachedNaturalIdValueSource.LOAD ); @@ -1808,7 +1813,7 @@ public class StatefulPersistenceContext implements PersistenceContext { final Object[] naturalIdValues = extractNaturalIdValues( state, persister ); // cache - naturalIdXrefDelegate.cacheNaturalIdCrossReference( persister, id, naturalIdValues ); + getNaturalIdXrefDelegate().cacheNaturalIdCrossReference( persister, id, naturalIdValues ); } @Override @@ -1965,7 +1970,7 @@ public class StatefulPersistenceContext implements PersistenceContext { persister = locateProperPersister( persister ); final Object[] naturalIdValues = getNaturalIdValues( state, persister ); - final Object[] localNaturalIdValues = naturalIdXrefDelegate.removeNaturalIdCrossReference( + final Object[] localNaturalIdValues = getNaturalIdXrefDelegate().removeNaturalIdCrossReference( persister, id, naturalIdValues @@ -2004,12 +2009,12 @@ public class StatefulPersistenceContext implements PersistenceContext { @Override public Object[] findCachedNaturalId(EntityPersister persister, Serializable pk) { - return naturalIdXrefDelegate.findCachedNaturalId( locateProperPersister( persister ), pk ); + return getNaturalIdXrefDelegate().findCachedNaturalId( locateProperPersister( persister ), pk ); } @Override public Serializable findCachedNaturalIdResolution(EntityPersister persister, Object[] naturalIdValues) { - return naturalIdXrefDelegate.findCachedNaturalIdResolution( locateProperPersister( persister ), naturalIdValues ); + return getNaturalIdXrefDelegate().findCachedNaturalIdResolution( locateProperPersister( persister ), naturalIdValues ); } @Override @@ -2047,7 +2052,7 @@ public class StatefulPersistenceContext implements PersistenceContext { @Override public Collection getCachedPkResolutions(EntityPersister entityPersister) { - return naturalIdXrefDelegate.getCachedPkResolutions( entityPersister ); + return getNaturalIdXrefDelegate().getCachedPkResolutions( entityPersister ); } @Override @@ -2060,6 +2065,7 @@ public class StatefulPersistenceContext implements PersistenceContext { persister = locateProperPersister( persister ); final Object[] naturalIdValuesFromCurrentObjectState = extractNaturalIdValues( entity, persister ); + final NaturalIdXrefDelegate naturalIdXrefDelegate = getNaturalIdXrefDelegate(); final boolean changed = ! naturalIdXrefDelegate.sameAsCached( persister, pk, @@ -2081,12 +2087,12 @@ public class StatefulPersistenceContext implements PersistenceContext { @Override public void cleanupFromSynchronizations() { - naturalIdXrefDelegate.unStashInvalidNaturalIdReferences(); + getNaturalIdXrefDelegate().unStashInvalidNaturalIdReferences(); } @Override public void handleEviction(Object object, EntityPersister persister, Serializable identifier) { - naturalIdXrefDelegate.removeNaturalIdCrossReference( + getNaturalIdXrefDelegate().removeNaturalIdCrossReference( persister, identifier, findCachedNaturalId( persister, identifier )