Repository: quarkusio/quarkus
Issue: Panache Reactive + Reloads

### Describe the bug

I have a very trivial project using:

```
✬ ArtifactId                                         Extension Name
✬ quarkus-hibernate-reactive-panache                 Hibernate Reactive with Panache
✬ quarkus-reactive-pg-client                         Reactive PostgreSQL client
✬ quarkus-resteasy-reactive                          RESTEasy Reactive
✬ quarkus-resteasy-reactive-jsonb                    RESTEasy Reactive JSON-B
```

And I have an `@ApplicationScoped` `FruitResource` using Panache as in this page:

<https://quarkus.io/guides/getting-started-reactive>

I *also* have a second `FruitResource` that uses Hibernate Reactive *without* Panache:

```java
@Path("/fruits")
@ApplicationScoped
public class FruitResource {

    @Inject
    Mutiny.SessionFactory factory;

    @GET
    public Uni<List<Fruit>> get() {
        return factory.withStatelessSession(s -> s.createQuery("from Fruit order by name", Fruit.class).getResultList());
    }

    @GET
    @Path("/{id}")
    public Uni<Fruit> getSingle(Long id) {
        return factory.withStatelessSession(s-> s.get(Fruit.class, id));
    }

    @POST
    public Uni<Response> create(Fruit fruit) {
        return factory.withStatelessTransaction(s -> s.insert(fruit))
                .replaceWith(Response.created(URI.create("/fruits/" + fruit.id)).build());
    }
}
```

After editing the `Fruit` class, to trigger a reload, the Panache resource is in a broken state, but the non-Panache resource keeps working perfectly. The error is:

```
java.lang.IllegalStateException: EntityManagerFactory is closed
	at org.hibernate.internal.SessionFactoryImpl.validateNotClosed(SessionFactoryImpl.java:641)
	at org.hibernate.internal.SessionFactoryImpl.getCache(SessionFactoryImpl.java:900)
	at org.hibernate.internal.AbstractSharedSessionContract.<init>(AbstractSharedSessionContract.java:166)
	at org.hibernate.internal.SessionImpl.<init>(SessionImpl.java:218)
	at org.hibernate.reactive.session.impl.ReactiveSessionImpl.<init>(ReactiveSessionImpl.java:173)
	at org.hibernate.reactive.mutiny.impl.MutinySessionFactoryImpl.lambda$openSession$1(MutinySessionFactoryImpl.java:111)
	at io.smallrye.context.impl.wrappers.SlowContextualSupplier.get(SlowContextualSupplier.java:21)
	at io.smallrye.mutiny.operators.uni.builders.UniCreateFromItemSupplier.subscribe(UniCreateFromItemSupplier.java:28)
	at io.smallrye.mutiny.operators.AbstractUni.subscribe(AbstractUni.java:36)
	at io.smallrye.mutiny.operators.uni.UniOnFailureFlatMap.subscribe(UniOnFailureFlatMap.java:31)
	at io.smallrye.mutiny.operators.AbstractUni.subscribe(AbstractUni.java:36)
	at io.smallrye.mutiny.operators.uni.UniOnItemTransformToUni$UniOnItemTransformToUniProcessor.performInnerSubscription(UniOnItemTransformToUni.java:81)
	at io.smallrye.mutiny.operators.uni.UniOnItemTransformToUni$UniOnItemTransformToUniProcessor.onItem(UniOnItemTransformToUni.java:57)
	at io.smallrye.mutiny.operators.uni.UniOperatorProcessor.onItem(UniOperatorProcessor.java:47)
	at io.smallrye.mutiny.operators.uni.builders.UniCreateFromCompletionStage$CompletionStageUniSubscription.forwardResult(UniCreateFromCompletionStage.java:63)
	at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863)
	at java.base/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841)
	at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
	at java.base/java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:2147)
	at io.vertx.core.Future.lambda$toCompletionStage$3(Future.java:384)
	at io.vertx.core.impl.future.FutureImpl$3.onSuccess(FutureImpl.java:141)
	at io.vertx.core.impl.future.FutureBase.emitSuccess(FutureBase.java:60)
	at io.vertx.core.impl.future.FutureImpl.tryComplete(FutureImpl.java:211)
	at io.vertx.core.impl.future.Mapping.onSuccess(Mapping.java:40)
	at io.vertx.core.impl.future.FutureBase.lambda$emitSuccess$0(FutureBase.java:54)
	at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:174)
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:167)
	at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:470)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:569)
	at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.base/java.lang.Thread.run(Thread.java:833)
```

It looks like Panache Reactive somehow retains a reference to the old `EntityManagerFactory` after a reload.

### Expected behavior

_No response_

### Actual behavior

_No response_

### How to Reproduce?

_No response_

### Output of `uname -a` or `ver`

_No response_

### Output of `java -version`

_No response_

### GraalVM version (if different from Java)

_No response_

### Quarkus version or git rev

_No response_

### Build tool (ie. output of `mvnw --version` or `gradlew --version`)

_No response_

### Additional information

_No response_

Changed files:
extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/PanacheHibernateRecorder.java
extensions/panache/hibernate-reactive-panache-common/deployment/src/main/java/io/quarkus/hibernate/reactive/panache/common/deployment/PanacheJpaCommonResourceProcessor.java
extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/SessionOperations.java

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 f2ab103f76e..8faf7a2510d 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<PanacheNamedQueryEntityClassBuildStep> 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<String> 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 3a1083a57e0..7774ff96348 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<String, Set<String>> 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 7f5655acf0c..99c15a8691d 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();
+    }
 }