From b08ae4cd149ae6a404b5d4de3a136f2a898f4d82 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Wed, 13 May 2020 17:12:33 +0100 Subject: [PATCH] HHH-14029 Improve 2LC post-load event invocations --- .../engine/spi/SessionFactoryDelegatingImpl.java | 6 ++++++ .../engine/spi/SessionFactoryImplementor.java | 6 ++++++ .../hibernate/internal/FastSessionServices.java | 11 ++++++++++- .../hibernate/internal/SessionFactoryImpl.java | 3 ++- .../loader/entity/CacheEntityLoaderHelper.java | 16 ++++------------ 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryDelegatingImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryDelegatingImpl.java index c01048dbec..349e28f9fc 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryDelegatingImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryDelegatingImpl.java @@ -46,6 +46,7 @@ import org.hibernate.exception.spi.SQLExceptionConverter; import org.hibernate.graph.spi.RootGraphImplementor; import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.factory.IdentifierGeneratorFactory; +import org.hibernate.internal.FastSessionServices; import org.hibernate.metadata.ClassMetadata; import org.hibernate.metadata.CollectionMetadata; import org.hibernate.metamodel.spi.MetamodelImplementor; @@ -388,6 +389,11 @@ public class SessionFactoryDelegatingImpl implements SessionFactoryImplementor, return delegate.iterateEntityNameResolvers(); } + @Override + public FastSessionServices getFastSessionServices() { + return delegate.getFastSessionServices(); + } + @Override public EntityPersister locateEntityPersister(Class byClass) { return delegate.locateEntityPersister( byClass ); diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryImplementor.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryImplementor.java index 2883700b22..0a78018a4f 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryImplementor.java @@ -36,6 +36,7 @@ import org.hibernate.event.spi.EventEngine; import org.hibernate.exception.spi.SQLExceptionConverter; import org.hibernate.graph.spi.RootGraphImplementor; import org.hibernate.id.IdentifierGenerator; +import org.hibernate.internal.FastSessionServices; import org.hibernate.metamodel.spi.MetamodelImplementor; import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.entity.EntityPersister; @@ -182,6 +183,11 @@ public interface SessionFactoryImplementor extends Mapping, SessionFactory, Quer return getMetamodel().getEntityNameResolvers(); } + /** + * @return the FastSessionServices instance associated with this SessionFactory + */ + FastSessionServices getFastSessionServices(); + /** * Contract for resolving this SessionFactory on deserialization */ diff --git a/hibernate-core/src/main/java/org/hibernate/internal/FastSessionServices.java b/hibernate-core/src/main/java/org/hibernate/internal/FastSessionServices.java index c4306f152d..7539eb6e46 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/FastSessionServices.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/FastSessionServices.java @@ -32,6 +32,8 @@ import org.hibernate.event.spi.LoadEventListener; import org.hibernate.event.spi.LockEventListener; import org.hibernate.event.spi.MergeEventListener; import org.hibernate.event.spi.PersistEventListener; +import org.hibernate.event.spi.PostLoadEvent; +import org.hibernate.event.spi.PostLoadEventListener; import org.hibernate.event.spi.RefreshEventListener; import org.hibernate.event.spi.ReplicateEventListener; import org.hibernate.event.spi.ResolveNaturalIdEventListener; @@ -80,7 +82,7 @@ import static org.hibernate.cfg.AvailableSettings.JPA_SHARED_CACHE_STORE_MODE; * * @author Sanne Grinovero */ -final class FastSessionServices { +public final class FastSessionServices { /** * Default session properties @@ -107,6 +109,9 @@ final class FastSessionServices { final EventListenerGroup eventListenerGroup_SAVE_UPDATE; final EventListenerGroup eventListenerGroup_UPDATE; + //Frequently used by 2LC initialization: + final EventListenerGroup eventListenerGroup_POST_LOAD; + //Intentionally Package private: final boolean disallowOutOfTransactionUpdateOperations; final boolean useStreamForLobBinding; @@ -155,6 +160,7 @@ final class FastSessionServices { this.eventListenerGroup_SAVE = listeners( eventListenerRegistry, EventType.SAVE ); this.eventListenerGroup_SAVE_UPDATE = listeners( eventListenerRegistry, EventType.SAVE_UPDATE ); this.eventListenerGroup_UPDATE = listeners( eventListenerRegistry, EventType.UPDATE ); + this.eventListenerGroup_POST_LOAD = listeners( eventListenerRegistry, EventType.POST_LOAD ); //Other highly useful constants: this.dialect = jdbcServices.getJdbcEnvironment().getDialect(); @@ -285,4 +291,7 @@ final class FastSessionServices { return defaultJdbcObservers; } + public void firePostLoadEvent(final PostLoadEvent postLoadEvent) { + eventListenerGroup_POST_LOAD.fireEventOnEachListener( postLoadEvent, PostLoadEventListener::onPostLoad ); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index dbb0114268..b2a806d71f 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -1658,7 +1658,8 @@ public class SessionFactoryImpl implements SessionFactoryImplementor { /** * @return the FastSessionServices for this SessionFactory. */ - FastSessionServices getFastSessionServices() { + @Override + public FastSessionServices getFastSessionServices() { return this.fastSessionServices; } diff --git a/hibernate-core/src/main/java/org/hibernate/loader/entity/CacheEntityLoaderHelper.java b/hibernate-core/src/main/java/org/hibernate/loader/entity/CacheEntityLoaderHelper.java index a8b32ffd13..30b404f8ac 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/entity/CacheEntityLoaderHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/entity/CacheEntityLoaderHelper.java @@ -36,6 +36,7 @@ import org.hibernate.event.spi.PostLoadEvent; import org.hibernate.event.spi.PostLoadEventListener; import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreMessageLogger; +import org.hibernate.internal.FastSessionServices; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.pretty.MessageHelper; import org.hibernate.proxy.HibernateProxy; @@ -354,22 +355,13 @@ public class CacheEntityLoaderHelper extends AbstractLockUpgradeEventListener { .setId( entityId ) .setPersister( persister ); - for ( PostLoadEventListener listener : postLoadEventListeners( session ) ) { - listener.onPostLoad( postLoadEvent ); - } + session.getSessionFactory() + .getFastSessionServices() + .firePostLoadEvent( postLoadEvent ); return entity; } - private Iterable postLoadEventListeners(EventSource session) { - return session - .getFactory() - .getServiceRegistry() - .getService( EventListenerRegistry.class ) - .getEventListenerGroup( EventType.POST_LOAD ) - .listeners(); - } - public static class PersistenceContextEntry { private final Object entity; private EntityStatus status;