Hibernate Reactive: sessions are not closed, connections leak
See original GitHub issueATM, we have the following method to produce Stage.Session
and Mutiny.Session
tied to the request scope:
package io.quarkus.hibernate.reactive.runtime;
import java.util.concurrent.CompletionStage;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import org.hibernate.reactive.mutiny.Mutiny;
import org.hibernate.reactive.stage.Stage;
import io.quarkus.arc.DefaultBean;
import io.smallrye.mutiny.Uni;
@ApplicationScoped
public class ReactiveSessionProducer {
@Inject
private Stage.SessionFactory reactiveSessionFactory;
@Inject
private Mutiny.SessionFactory mutinySessionFactory;
@Produces
@RequestScoped
@DefaultBean
public CompletionStage<Stage.Session> stageSession() {
return reactiveSessionFactory.openSession();
}
@Produces
@RequestScoped
@DefaultBean
public Uni<Mutiny.Session> mutinySession() {
return mutinySessionFactory.openSession();
}
public void disposeStageSession(@Disposes CompletionStage<Stage.Session> reactiveSession) {
reactiveSession.whenComplete((s, t) -> {
if (s != null)
s.close();
});
}
public void disposeMutinySession(@Disposes Uni<Mutiny.Session> reactiveSession) {
// TODO: @AGG this is not working, need to check w/ Clement to figure out the proper way
reactiveSession.on().termination((session, ex, cancelled) -> {
if (session != null)
session.close();
});
}
}
Now, the problem is that we’re tying a CompletionStage<Stage.Session>
(and respectively same for Mutiny) to the request, which is a promise of a session.
The disposeStageSession
cannot work, because CompletionStage
are immutable, and the value returned by reactiveSession.whenComplete()
is never used, so this s.close()
will never be called.
A similar issue exists for disposeMutinySession
except it’s possible that the Uni<Mutiny.Session>
opens a new session for each call to subscribe
(unless cache()
is called on it, I don’t know if it is) whereas for CompletionStage
it’s eager and will have a single session by design.
We can’t do whenComplete
/on().termination
in stageSession
/mutinySession
either because that would close the session before we return it to the user.
To be frank, ATM I don’t know how to register a way to auto-close what is lazily obtained by a CS/Uni and tied to the request scope.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (5 by maintainers)
@FroMage I have a fix for this one. Will send a PR soon. Yet I still don’t know why the Hibernate Reactive test pass on
master
…When the producer is invoked the stage session is created and the CS will at some point be fulfilled, exactly.
As for the Mutiny session it is different. We can’t use
onTermination
because it creates another coldUni
and nothing happens until someone subscribes to it.