hibernate的SessionFactory

mike_zp59 2006-07-10 05:49:50
我们都知道hibernate访问数据库是通过SessionFactory的session对象来操作的.但是SessionFactory是一个重量级的对象,会消耗很多系统资源,如果在每个DAO里都新建一个SessionFactory,无意性能会大为降低,所以怎么才可以创建一个应用系统范围内的唯一的SessionFactory?
难道只能用JNDI吗?还有没有别的方法?希望各位大哥大姐不吝赐教,最好有点例子!
谢谢啦!
...全文
599 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
iriver103 2006-07-10
  • 打赏
  • 举报
回复
楼上正解。
IceCraft 2006-07-10
  • 打赏
  • 举报
回复
楼主是否了解单例和原型模式。
你可以使用单例来实现一个全局的session工厂。以下是一个HibernateSessionFactory的代码,你可以参考或直接使用:
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {

private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";

/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();

/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();

/** The single instance of hibernate SessionFactory */
private static org.hibernate.SessionFactory sessionFactory;

/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

return session;
}

/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);

if (session != null) {
session.close();
}
}

/**
* Default constructor.
*/
private HibernateSessionFactory() {
}

}


可以使用其中的静态方法currentSession获得一个session,使用closeSession来关闭session。
你也需要了解以下本地线程变量技术的使用,这个hsf就使用了这个技术ThreadLocal。也就是说在用户的一次请求服务的过程中,每次需要用到session进行操作时,调用currentSession方法,注意你不必手动去session.close()。只要用到session就调用currentSession。直到最后你真的不需要session时再调用closeSession。
如此处理之后,一是极大提高性能,二是你可以轻松使用hibernate的lazy=true延迟加载,也是极大的提高性能。
为了避免手工调用closeSession的麻烦以及由于疏忽而没有调用到closeSession,你可以做一个过滤器filter,当用户请求结束时,自动调用closeSession。

通过以上的配置,你的程序开发将非常方便,每次用到session时就HibernateSessionFactory.currentSession()来获取session,而不必考虑去关闭它,系统会自己做掉。

如果你在使用spring,如楼上所说,可以用spring提供的HibernateTemplate来封装获取session的这个过程,至于自动关闭session,spring也提供了一个org.springframework.orm.hibernate3.support.OpenSessionInViewFilter这样的过滤器,就不用自己写一个过滤器了。
Ryo_Hazuki 2006-07-10
  • 打赏
  • 举报
回复
1.static
2.spring hibernatetemplate

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧