会议和工厂应该关闭吗?

时间:2015-10-20 12:14:54

标签: java hibernate hibernate-5.x

我正在使用Hibernate 5.0.2.Final与数据源连接(在Tomcat 8.0.15上)并开始问自己是否有必要不仅关闭Session而且还关闭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;
    }

hibernate.cfg.xml

中的一些细节
<property name="hibernate.connection.datasource">java:comp/env/jdbc/sqlserv</property>        
<property name="current_session_context_class">thread</property> 
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 
<property name="hbm2ddl.auto">auto</property> 
<property name="show_sql">false</property>       
<property name="hibernate.generate_statistics">true</property>  

和HibernateUtil:

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;
}
}

我不确定是否有必要在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);
    }
}

1 个答案:

答案 0 :(得分:2)

您应该在每个查询上不关闭您的 10.0.0.97 - - [25/Oct/2015:09:25:12 +0000] "GET /browse/123 HTTP/1.1" 404 715 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36" 。每个应用程序只应初始化一次SessionFactory

来自hibernate documentation

  

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

     

实现者必须是线程安全的。