Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 会话和工厂是否应该关闭?_Java_Hibernate_Hibernate 5.x - Fatal编程技术网

Java 会话和工厂是否应该关闭?

Java 会话和工厂是否应该关闭?,java,hibernate,hibernate-5.x,Java,Hibernate,Hibernate 5.x,我正在使用Hibernate 5.0.2.Final和数据源连接(在Tomcat 8.0.15上),并开始问自己是否不仅需要关闭会话,还需要关闭SessionFactory 现在看起来是这样的: public static List<HibernateList> getHibernateList() { Session session = null; final String hql = "SELECT H FROM myhibernate.MyHib

我正在使用Hibernate 5.0.2.Final和数据源连接(在Tomcat 8.0.15上),并开始问自己是否不仅需要关闭会话,还需要关闭SessionFactory

现在看起来是这样的:

public static List<HibernateList> getHibernateList() {
        Session session = null;
        final String hql = "SELECT H FROM myhibernate.MyHibernate";
        try {
            SessionFactory factory = HibernateUtil.getSessionFactory();
            session = factory.openSession();
            session.beginTransaction();

            Query query = session.createQuery(hql);

            return query.list();
        } catch (HibernateException hibex) {
            Logger.getLogger(Hibernatepicker.class.getName()).log(Level.INFO, null, hql);
            Logger.getLogger(Hibernatepicker.class.getName()).log(Level.SEVERE, null, hibex);
        } finally {
            try {
                if (session != null) {
                    session.close();
                }
            } catch (HibernateException hibex) {
            }//Nothing I could do...
        }
        return null;
    }
我还不确定是否有必要在finally块中调用此方法,而不是仅关闭会话:

public static void disconnect(Session session, SessionFactory factory) {
        try {
            if (session != null) {
                session.close();
            } else {
                Logger.getLogger(HibernateUtil.class.getName()).log(Level.INFO, null, "Session is Null");
        }

    } catch (HibernateException | NullPointerException hibex) {
        Logger.getLogger(HibernateUtil.class.getName()).log(Level.INFO, null, "Couldn't close session, but there's nothing we can do...");
        Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE, null, hibex);
    }
    try {
        if (factory != null) {
            factory.close();
        } else {
            Logger.getLogger(HibernateUtil.class.getName()).log(Level.INFO, null, "Factory is Null");
        }

    } catch (HibernateException | NullPointerException hibex) {
        Logger.getLogger(HibernateUtil.class.getName()).log(Level.INFO, null, "Couldn't close session, but there's nothing we can do...");
        Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE, null, hibex);
    }
}

您应该不要在每次查询时关闭
SessionFactory
。您的
SessionFactory
每个应用程序只能初始化一次

这里的主要约定是创建会话实例。通常 应用程序只有一个SessionFactory实例和线程 服务客户端请求从此工厂获取会话实例。 SessionFactory的内部状态是不可变的。一旦是 此内部状态已设置。这种内部状态包括所有 关于对象/关系映射的元数据

实现者必须是线程安全的


太好了,谢谢你的快速回复。有没有办法确保它只初始化一次?@Qohelet有。您可以使用单例模式来确保单次初始化,或者如果您使用的是任何CDI环境(如Spring等),它们都支持这种内置功能。。。?我的假设正确吗?做得对吗?
public class HibernateUtil {
private static final SessionFactory sessionFactory;

static {
    try {
        Configuration cfg = new Configuration();
        sessionFactory = cfg.configure("hibernate.cfg.xml").buildSessionFactory();
    } catch (Throwable ex) {
        Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE, null, ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}
}
public static void disconnect(Session session, SessionFactory factory) {
        try {
            if (session != null) {
                session.close();
            } else {
                Logger.getLogger(HibernateUtil.class.getName()).log(Level.INFO, null, "Session is Null");
        }

    } catch (HibernateException | NullPointerException hibex) {
        Logger.getLogger(HibernateUtil.class.getName()).log(Level.INFO, null, "Couldn't close session, but there's nothing we can do...");
        Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE, null, hibex);
    }
    try {
        if (factory != null) {
            factory.close();
        } else {
            Logger.getLogger(HibernateUtil.class.getName()).log(Level.INFO, null, "Factory is Null");
        }

    } catch (HibernateException | NullPointerException hibex) {
        Logger.getLogger(HibernateUtil.class.getName()).log(Level.INFO, null, "Couldn't close session, but there's nothing we can do...");
        Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE, null, hibex);
    }
}