会话和工厂应该关闭吗?
问题描述:
我正在使用Hibernate 5.0.2.Final与数据源连接(在Tomcat 8.0.15上),并开始问我自己是否有必要关闭Session而且还需要SessionFactory?会话和工厂应该关闭吗?
现在它看起来是这样的:
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 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中的一些细节有必要或不要在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
应仅在每个应用程序中初始化一次。
这里的主要合同是创建Session实例。通常, 应用程序具有一个SessionFactory实例,并且服务于客户端请求的线程 从该工厂获取Session实例。 SessionFactory的内部状态是不可变的。一旦创建了这个内部状态 被设置。该内部状态包括关于对象/关系映射的元数据的所有 。
执行者必须是线程安全的。
太好了,谢谢你的快速响应。有什么方法可以确保它刚刚初始化一次? – Qohelet
@Qohelet是的。您可以使用Singleton Pattern来确保单个初始化,或者如果您使用任何CDI环境(如Spring等),它们都支持这种内置的功能。 – bhdrkn
我的HibernateUtil看起来有点像Singelton-ish ...?我的假设是正确的吗? – Qohelet