From f434b323d685e9b15ace036e0284e6dd40ffcd8f Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Fri, 5 May 2023 17:00:45 +1000 Subject: [PATCH] Fix Hibernate Reactive dev mode The session is being cached in a static so after dev mode restart users get a closed session. (cherry picked from commit fcd28088b154c6bc5c1348e7c018409f4bdbf60d) --- .../deployment/PanacheJpaCommonResourceProcessor.java | 7 +++++++ .../common/runtime/PanacheHibernateRecorder.java | 10 ++++++++++ .../panache/common/runtime/SessionOperations.java | 4 ++++ 3 files changed, 21 insertions(+) diff --git a/extensions/panache/hibernate-reactive-panache-common/deployment/src/main/java/io/quarkus/hibernate/reactive/panache/common/deployment/PanacheJpaCommonResourceProcessor.java b/extensions/panache/hibernate-reactive-panache-common/deployment/src/main/java/io/quarkus/hibernate/reactive/panache/common/deployment/PanacheJpaCommonResourceProcessor.java index f2ab103f76e41..8faf7a2510d68 100644 --- a/extensions/panache/hibernate-reactive-panache-common/deployment/src/main/java/io/quarkus/hibernate/reactive/panache/common/deployment/PanacheJpaCommonResourceProcessor.java +++ b/extensions/panache/hibernate-reactive-panache-common/deployment/src/main/java/io/quarkus/hibernate/reactive/panache/common/deployment/PanacheJpaCommonResourceProcessor.java @@ -41,6 +41,7 @@ import io.quarkus.deployment.annotations.ExecutionTime; import io.quarkus.deployment.annotations.Record; import io.quarkus.deployment.builditem.CombinedIndexBuildItem; +import io.quarkus.deployment.builditem.ShutdownContextBuildItem; import io.quarkus.deployment.util.JandexUtil; import io.quarkus.gizmo.ClassCreator; import io.quarkus.hibernate.orm.deployment.HibernateOrmEnabled; @@ -208,6 +209,12 @@ void buildNamedQueryMap(List namedQueryEn panacheHibernateRecorder.setNamedQueryMap(namedQueryMap); } + @BuildStep + @Record(ExecutionTime.RUNTIME_INIT) + public void shutdown(ShutdownContextBuildItem shutdownContextBuildItem, PanacheHibernateRecorder panacheHibernateRecorder) { + panacheHibernateRecorder.clear(shutdownContextBuildItem); + } + private void lookupNamedQueries(CombinedIndexBuildItem index, DotName name, Set namedQueries) { ClassInfo classInfo = index.getComputingIndex().getClassByName(name); if (classInfo == null) { diff --git a/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/PanacheHibernateRecorder.java b/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/PanacheHibernateRecorder.java index 3a1083a57e0da..7774ff963487d 100644 --- a/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/PanacheHibernateRecorder.java +++ b/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/PanacheHibernateRecorder.java @@ -3,6 +3,7 @@ import java.util.Map; import java.util.Set; +import io.quarkus.runtime.ShutdownContext; import io.quarkus.runtime.annotations.Recorder; @Recorder @@ -10,4 +11,13 @@ public class PanacheHibernateRecorder { public void setNamedQueryMap(Map> namedQueryMap) { NamedQueryUtil.setNamedQueryMap(namedQueryMap); } + + public void clear(ShutdownContext shutdownContext) { + shutdownContext.addShutdownTask(new Runnable() { + @Override + public void run() { + SessionOperations.clear(); + } + }); + } } diff --git a/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/SessionOperations.java b/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/SessionOperations.java index 7f5655acf0c59..99c15a8691d11 100644 --- a/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/SessionOperations.java +++ b/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/SessionOperations.java @@ -205,4 +205,8 @@ static Mutiny.SessionFactory getSessionFactory() { return SESSION_FACTORY.get(); } + static void clear() { + SESSION_FACTORY.clear(); + SESSION_KEY.clear(); + } }