All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.hibernate.orm.runtime.RequestScopedSessionHolder Maven / Gradle / Ivy

package io.quarkus.hibernate.orm.runtime;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PreDestroy;
import javax.enterprise.context.RequestScoped;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * Bean that is used to manage request scoped sessions
 */
@RequestScoped
public class RequestScopedSessionHolder {

    private final Map sessions = new HashMap<>();

    public Session getOrCreateSession(String name, SessionFactory factory) {
        return sessions.computeIfAbsent(name, (n) -> factory.openSession());
    }

    @PreDestroy
    public void destroy() {
        for (Map.Entry entry : sessions.entrySet()) {
            entry.getValue().close();
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy