当前位置: 代码迷 >> Java Web开发 >> SessionFactory 怎么共享一个实例.
  详细解决方案

SessionFactory 怎么共享一个实例.

热度:8438   发布时间:2013-02-25 21:17:58
SessionFactory 如何共享一个实例...
书上说一个应用可以的话,尽量共享一个SessionFactory实例
我知道用static的方法来共享,但是怎么确保SessionFactory实例在第一次被调用前一定会执行一次呢?

举个例子,下面是网上看到的方法:
Java code
    import org.hibernate.*;             import org.hibernate.cfg.*;             public class CoreSession {             static SessionFactory sessionFactory;       //注意到这里的SessionFactory都是static的    //初始化Hibernate,创建SessionFactory实例,只在该类被加载到内存时执行一次             static{                 try{                      Configuration config = new Configuration().configure();                      sessionFactory = config.buildSessionFactory();                 } catch (Exception e) {                     System.out.println(e.getMessage());                 }             }             }   




这种情况下,怎么来保证在调用SessionFactory实例之前,这个构建过程一定会被执行一次呢?

------解决方案--------------------------------------------------------


Java code
public class CoreSession {            private static SessionFactory sessionFactory;      public static SessionFactory getSessionFactory(){       if(sessionFactory == null) {             try{                    Configuration config = new Configuration().configure();                   sessionFactory = config.buildSessionFactory();                      } catch (Exception e) {                          e.printStackTrace();             }        }        return sessionFactory;   }}
------解决方案--------------------------------------------------------
static {}静态块代码会在classloader加载这个类的时候就被执行了,所以在使用时候完全可以放心,不用担心它没有被初始化的问题!
------解决方案--------------------------------------------------------
Java code
package dao;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.cfg.Configuration;public class SessionFactory {    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();    private static Configuration configuration = new Configuration();    private static org.hibernate.SessionFactory sessionFactory;    private static String configFile = CONFIG_FILE_LOCATION;    static {        try {            configuration.configure(configFile);            sessionFactory = configuration.buildSessionFactory();        } catch (Exception e) {            System.err.println("%%%% Error Creating SessionFactory %%%%");            e.printStackTrace();        }    }    private SessionFactory() {    }    public static Session getSession() throws HibernateException {        Session session = (Session) threadLocal.get();        if (session == null || !session.isOpen()) {            if (sessionFactory == null) {                rebuildSessionFactory();            }            session = (sessionFactory != null) ? sessionFactory.openSession()                    : null;            threadLocal.set(session);        }        return session;    }    public static void rebuildSessionFactory() {        try {            configuration.configure(configFile);            sessionFactory = configuration.buildSessionFactory();        } catch (Exception e) {            System.err.println("%%%% Error Creating SessionFactory %%%%");            e.printStackTrace();        }    }    public static void closeSession() {        Session session = null;        try {            session = (Session) threadLocal.get();            threadLocal.set(null);            if (session != null) {                session.close();            }        } catch (Exception e) {            e.printStackTrace();        } finally {            if (session != null) {                session.close();            }        }    }    public static org.hibernate.SessionFactory getSessionFactory() {        return sessionFactory;    }    public static void setConfigFile(String configFile) {        SessionFactory.configFile = configFile;        sessionFactory = null;    }    public static Configuration getConfiguration() {        return configuration;    }}
  相关解决方案
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!