From 52ca2703f30acb470346619bb433c9b38951a0fc Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Wed, 5 Feb 2020 09:55:14 +0000 Subject: [PATCH] HHH-13850 Clear the BytecodeProvider caches both after SessionFactory creation and stop --- .../internal/SessionFactoryBuilderImpl.java | 6 ++++ ...ionFactoryObserverForBytecodeEnhancer.java | 35 +++++++++++++++++++ .../internal/bytebuddy/ByteBuddyState.java | 2 +- .../internal/SessionFactoryImpl.java | 2 -- 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 hibernate-core/src/main/java/org/hibernate/bytecode/internal/SessionFactoryObserverForBytecodeEnhancer.java diff --git a/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryBuilderImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryBuilderImpl.java index bc8a5f7928..f13f319a1a 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryBuilderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryBuilderImpl.java @@ -20,10 +20,13 @@ import org.hibernate.SessionFactory; import org.hibernate.SessionFactoryObserver; import org.hibernate.boot.SessionFactoryBuilder; import org.hibernate.boot.TempTableDdlTransactionHandling; +import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.spi.BootstrapContext; import org.hibernate.boot.spi.MetadataImplementor; import org.hibernate.boot.spi.SessionFactoryBuilderImplementor; import org.hibernate.boot.spi.SessionFactoryOptions; +import org.hibernate.bytecode.internal.SessionFactoryObserverForBytecodeEnhancer; +import org.hibernate.bytecode.spi.BytecodeProvider; import org.hibernate.cache.spi.TimestampsCacheFactory; import org.hibernate.context.spi.CurrentTenantIdentifierResolver; import org.hibernate.dialect.function.SQLFunction; @@ -459,6 +462,9 @@ public class SessionFactoryBuilderImpl implements SessionFactoryBuilderImplement @Override public SessionFactory build() { metadata.validate(); + final StandardServiceRegistry serviceRegistry = metadata.getMetadataBuildingOptions().getServiceRegistry(); + BytecodeProvider bytecodeProvider = serviceRegistry.getService( BytecodeProvider.class ); + addSessionFactoryObservers( new SessionFactoryObserverForBytecodeEnhancer( bytecodeProvider ) ); return new SessionFactoryImpl( metadata, buildSessionFactoryOptions() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/internal/SessionFactoryObserverForBytecodeEnhancer.java b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/SessionFactoryObserverForBytecodeEnhancer.java new file mode 100644 index 0000000000..7a84b3b038 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/SessionFactoryObserverForBytecodeEnhancer.java @@ -0,0 +1,35 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.bytecode.internal; + +import org.hibernate.SessionFactory; +import org.hibernate.SessionFactoryObserver; +import org.hibernate.bytecode.spi.BytecodeProvider; + +public final class SessionFactoryObserverForBytecodeEnhancer implements SessionFactoryObserver { + + private final BytecodeProvider bytecodeProvider; + + public SessionFactoryObserverForBytecodeEnhancer(BytecodeProvider bytecodeProvider) { + this.bytecodeProvider = bytecodeProvider; + } + + @Override + public void sessionFactoryCreated(final SessionFactory factory) { + this.bytecodeProvider.resetCaches(); + } + + @Override + public void sessionFactoryClosing(final SessionFactory factory) { + this.bytecodeProvider.resetCaches(); + } + + @Override + public void sessionFactoryClosed(final SessionFactory factory) { + this.bytecodeProvider.resetCaches(); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/ByteBuddyState.java b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/ByteBuddyState.java index f17e48b0b5..e0f7122917 100644 --- a/hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/ByteBuddyState.java +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/ByteBuddyState.java @@ -159,7 +159,7 @@ public final class ByteBuddyState { /** * Wipes out all known caches used by ByteBuddy. This implies it might trigger the need - * to re-create some helpers if used at runtime, especially as this state is shared by + * to re-create some helpers if used at runtime, especially as this state might be shared by * multiple SessionFactory instances, but at least ensures we cleanup anything which is no * longer needed after a SessionFactory close. * The assumption is that closing SessionFactories is a rare event; in this perspective the cost 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 06c5aa3760..4bb5553dd4 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -753,8 +753,6 @@ public class SessionFactoryImpl implements SessionFactoryImplementor { * @throws HibernateException */ public void close() throws HibernateException { - //This is an idempotent operation so we can do it even before the checks (it won't hurt): - Environment.getBytecodeProvider().resetCaches(); synchronized (this) { if ( isClosed ) { if ( getSessionFactoryOptions().getJpaCompliance().isJpaClosedComplianceEnabled() ) {