会议和工厂应该关闭吗? [英] Should session and factory be closed?

查看:120
本文介绍了会议和工厂应该关闭吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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();

查询查询= session.createQuery(hql);

返回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){
} //无我能做...
}
return null;

来自hibernate.cfg.xml的一些细节

 < property name =hibernate.connection.datasource> java:comp / env / jdbc / sqlserv< / property> 
< property name =current_session_context_class>线程< / 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);
抛出新的ExceptionInInitializerError(ex);



public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}

我不确定是否需要致电这个方法在finally块中,而不是只关闭会话:

  public static void disconnect(Session session,SessionFactory factory){ 
尝试{
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'闭会,但我们没有办法......);
Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE,null,hibex);
}
尝试{
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'闭会,但我们没有办法......);
Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE,null,hibex);



$ div $解析方案

你应在每个查询中不要关闭您的 SessionFactory 。您的 SessionFactory 应该仅为每个应用程序初始化一次。



来自 hibernate documentation


这里的主要契约是创建Session实例。通常
应用程序具有一个SessionFactory实例,并且线程
服务于客户端请求,从该工厂获取Session实例。
SessionFactory的内部状态是不可变的。一旦它创建了
,就设置了这个内部状态。这个内部状态包含所有关于对象/关系映射的
元数据。



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


I'm using Hibernate 5.0.2.Final with a Data-Source connection (On a Tomcat 8.0.15) and started to ask myself if it's necessary to not only close the Session but also the SessionFactory?

Right now it looks like this:

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

Some details from the 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>  

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

I'm undecided whether it's necessary or not to call this method in the finally-block instead of only closing the session:

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

解决方案

You should not close your SessionFactory on every query. Your SessionFactory should be initialised only once per application.

From the hibernate documentation.

The main contract here is the creation of Session instances. Usually an application has a single SessionFactory instance and threads servicing client requests obtain Session instances from this factory. The internal state of a SessionFactory is immutable. Once it is created this internal state is set. This internal state includes all of the metadata about Object/Relational Mapping.

Implementors must be threadsafe.

这篇关于会议和工厂应该关闭吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆