question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hibernate Reactive: sessions are not closed, connections leak

See original GitHub issue

ATM, 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:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tsegismontcommented, Jun 15, 2020

@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

0reactions
tsegismontcommented, Jun 16, 2020

Ah, it’s called because as soon as it is created, it will be produced, and so at some point it will be fullfilled and we can chain on it, I guess you’re right.

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 cold Uni and nothing happens until someone subscribes to it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How we fixed all database connection leaks - In Relation To
The first thing to notice is the lack of a try/finally block which should be closing the session even if there is an...
Read more >
How to fix connection leaks in hibernate? - java - Stack Overflow
When using the Hibernate session you must make sure to ALWAYS close it, no matter what. Meaning your method should use the try-finally...
Read more >
All configuration options - Quarkus
AWS Lambda Type Default AWS Lambda Common Type Default AWS Lambda Gateway REST API Type Default Agroal ‑ Database connection pool Type Default
Read more >
Introduction to HikariCP - Baeldung
We learn about the HikariCP JDBC connection pool project. ... Learn how to add c3p0 to a Hibernate application and configure some common ......
Read more >
How to Prevent JDBC Resource Leaks with JDBC ... - jOOQ blog
No exceptions, though, and when the peak load was gone in the evening, ... Utility method doesn't have to close the connection:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found