`

Hibernate的Session管理之HibernateSessionFactory

阅读更多
myeclipse 生成的hibernateUtil类,感觉实现比较好。就放到这里。
java 代码
  1. package net.karison.dao.hibernate.util;   
  2.   
  3. import org.hibernate.HibernateException;   
  4. import org.hibernate.Session;   
  5. import org.hibernate.cfg.Configuration;   
  6.   
  7. /**  
  8.  * Configures and provides access to Hibernate sessions, tied to the  
  9.  * current thread of execution.  Follows the Thread Local Session  
  10.  * pattern, see {@link http://hibernate.org/42.html }.  
  11.  */  
  12. public class HibernateSessionFactory {   
  13.   
  14.     /**   
  15.      * Location of hibernate.cfg.xml file.  
  16.      * Location should be on the classpath as Hibernate uses    
  17.      * #resourceAsStream style lookup for its configuration file.   
  18.      * The default classpath location of the hibernate config file is   
  19.      * in the default package. Use #setConfigFile() to update   
  20.      * the location of the configuration file for the current session.     
  21.      */  
  22.     private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";   
  23.     private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();   
  24.     private  static Configuration configuration = new Configuration();   
  25.     private static org.hibernate.SessionFactory sessionFactory;   
  26.     private static String configFile = CONFIG_FILE_LOCATION;   
  27.   
  28.     private HibernateSessionFactory() {   
  29.     }   
  30.        
  31.     /**  
  32.      * Returns the ThreadLocal Session instance.  Lazy initialize  
  33.      * the <code>SessionFactory</code> if needed.  
  34.      *  
  35.      *  @return Session  
  36.      *  @throws HibernateException  
  37.      */  
  38.     public static Session getSession() throws HibernateException {   
  39.         Session session = (Session) threadLocal.get();   
  40.   
  41.         if (session == null || !session.isOpen()) {   
  42.             if (sessionFactory == null) {   
  43.                 rebuildSessionFactory();   
  44.             }   
  45.             session = (sessionFactory != null) ? sessionFactory.openSession()   
  46.                     : null;   
  47.             threadLocal.set(session);   
  48.         }   
  49.   
  50.         return session;   
  51.     }   
  52.   
  53.     /**  
  54.      *  Rebuild hibernate session factory  
  55.      *  
  56.      */  
  57.     public static void rebuildSessionFactory() {   
  58.         try {   
  59.             configuration.configure(configFile);   
  60.             sessionFactory = configuration.buildSessionFactory();   
  61.         } catch (Exception e) {   
  62.             System.err   
  63.                     .println("%%%% Error Creating SessionFactory %%%%");   
  64.             e.printStackTrace();   
  65.         }   
  66.     }   
  67.   
  68.     /**  
  69.      *  Close the single hibernate session instance.  
  70.      *  
  71.      *  @throws HibernateException  
  72.      */  
  73.     public static void closeSession() throws HibernateException {   
  74.         Session session = (Session) threadLocal.get();   
  75.         threadLocal.set(null);   
  76.   
  77.         if (session != null) {   
  78.             session.close();   
  79.         }   
  80.     }   
  81.   
  82.     /**  
  83.      *  return session factory  
  84.      *  
  85.      */  
  86.     public static org.hibernate.SessionFactory getSessionFactory() {   
  87.         return sessionFactory;   
  88.     }   
  89.   
  90.     /**  
  91.      *  return session factory  
  92.      *  
  93.      *  session factory will be rebuilded in the next call  
  94.      */  
  95.     public static void setConfigFile(String configFile) {   
  96.         HibernateSessionFactory.configFile = configFile;   
  97.         sessionFactory = null;   
  98.     }   
  99.   
  100.     /**  
  101.      *  return hibernate configuration  
  102.      *  
  103.      */  
  104.     public static Configuration getConfiguration() {   
  105.         return configuration;   
  106.     }   
  107.   
  108. }  
特注:在这种方式下,session的管理交给了threadlocal,因此在我们使用事务提交结束后可以自动关闭,我们无须再关闭session。
分享到:
评论
2 楼 jitabc 2008-12-08  
houlc 写道

想问一下,这样在myeclipse生成的dao里select save update delete 操作都不用手动关闭session吗,这种方法还需要在配置文件中作什么配置吗?

不用再手动关闭了,直接调用这个工具类就可以了
1 楼 houlc 2008-08-24  
想问一下,这样在myeclipse生成的dao里select save update delete 操作都不用手动关闭session吗,这种方法还需要在配置文件中作什么配置吗?

相关推荐

    HibernateSessionFactory.java Hibernate使用的整合的工具文件

    Session s= HibernateSessionFactory.getSession(); 就是Hibernate的工具java类

    HibernateSessionFactory

    用于获得Session会话及关闭Session会话

    spring配置sessionFactory(spring3.2.3+hibernate4.2.2)

    一个实例小工程,讲解的是将hibernate的sessionFactory交给spring管理的配置方法

    hibernate session.doc

    例如以下代码先加载一个持久化对象,然后通过delete()方法将它删除: Session session1 = sessionFactory.openSession(); Transaction tx1 = session1.beginTransaction(); // 先加载一个持久化对象 Customer ...

    新Hibernate SessionFactory().getCurrentSession()猫腻

    NULL 博文链接:https://zgdkik.iteye.com/blog/1835667

    J2EE利用Hibernate采用B/S架构网页设计

    public class HibernateSessionFactory { /** * Location of hibernate.cfg.xml file. * Location should be on the classpath as Hibernate uses * #resourceAsStream style lookup for its configuration ...

    Hibernate Web应用的开发步骤

    (5)创建Hibernate的SessionFactory类。 (6)通过SessionFactory创建Session实例。 (7)通过创建的Session实例进行持久化对象的管理。 (8)通过创建的Transaction实例进行事务管理。 (9)通过创建的Query或...

    Hibernate体系结构的概要图

    Hibernate的核心接口一共有6个,分别为:Session、SessionFactory、Transaction、Query、Criteria和Configuration。这6个核心接口在任何开发中都会用到。通过这些接口,不仅可以对持久化对象进行存取,还能够进行事务...

    hibernate3

    (2)SessionFactory:初始化Hibernate,充当数据存储源的代理,创建Session对象。一个SessinFactory实例对应一个数据存储源,应用从SessionFactory中获得Session实例。如果应用同时访问多个DB,怎需要为每个数据库...

    利用Spring来管理Hibernate完整例子

    其中Hibernate每次都需要手动创建SessionFactory,Session,手动开启提交关闭事务。而这一切操作完全是由Spring来代替。使持久层更加方便,使开发人员减少持久层操作,把注意力放到业务上。

    HibernateUtil.java Hibernate5.2.1

    Hibernate5.2.1 的工具类 创建session 和 sessionFactory

    Hibernate学习笔记和资料

    hibernate概述,hibernate入门Demo,hibernate配置文件详解(全局配置,实体类映射配置),配置实体规则,核心API详解(Configuration,sessionFactory,session,Transaction),hibernate中的对象状态以及刷新能缓存机制 ...

    第24次课-1 Spring与Hibernate的整合

    Spring通过ApplicationContext管理SessionFactory,可以不使用Hibernate应用必需的hibernate.cfg.xml。 Spring配置管理SessionFactory与数据库的连接,在实际的应用中,数据源会采用依赖注入的方式,传递给...

    hibernate操作数据库笔记

    注:由于SessionFactory是重量级对象,很耗资源,所以在获取SessionFactory对象时最好用单例模式获取,以确保一个应用中只有一个SessionFactory对象存在和线程安全,由于Session对象 是非线程安全的(尽管创建一个...

    hibernate基础教程

    在Hibernate中使用两极缓存结构,第一级缓存是Session级别的缓存,Session级别的缓存是由Hibernate管理的,一般情况下无需进行干预.第二级缓存是 SessionFactory级别的缓存,SessionFactory级的缓存可以进行配置和...

    hibernate4.3完整jar包

    Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java...Hibernate的核心接口一共有6个,分别为:Session、SessionFactory、Transaction、Query、Criteria和Configuration。

    Hibernate3使用经验

    ---------------Hibernate3.0 配置-------------- 1.Hibernate中配置参数 /** * 注意:HQL中使用参数的方法: ...SessionFactory sf = config.buildSessionFactory(); Session session = sf.openSession();

    Spring_Hibernate集成

    Hibernate Session的轻量级封装 * 默认情况下运行期异常才会回滚(包括继承了RuntimeException子类),普通异常是不会滚的 * 编写业务逻辑方法时,最好将异常一直向上抛出,在表示层(struts)处理 * 关于事务...

    Hibernate的五个核心接口介绍.png

    Hibernate的五个核心接口 Configuration SessionFactory Session Transcation Query和Criteria

    hibernate精华教程

    (2)SessionFactory:初始化Hibernate,充当数据存储源的代理,创建Session对象。一个SessinFactory实例对应一个数据存储源,应用从SessionFactory中获得Session实例。如果应用同时访问多个DB,怎需要为每个数据库...

Global site tag (gtag.js) - Google Analytics