private构造函数(程序设计)
来源:优易学  2011-1-7 10:04:54   【优易学:中国教育考试门户网】   资料下载   IT书店

  看下面的类:
  HibernateSessionFactory.java
  package zy.pro.wd.util;
  import net.sf.hibernate.HibernateException;
  import net.sf.hibernate.Session;
  import net.sf.hibernate.cfg.Configuration;
  public class HibernateSessionFactory {
  /**
  * Location of hibernate.cfg.xml file.
  * NOTICE: Location should be on the classpath as Hibernate uses
  * #resourceAsStream style lookup for its configuration file. That
  * is place the config file in a Java package - the default location
  * is the default Java package.
  * Examples:
  * CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
  * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".
  */
  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 net.sf.hibernate.SessionFactory sessionFactory;
  /**
  * Returns the ThreadLocal Session instance. Lazy initialize
  * the SessionFactory if needed.
  *
  * @return Session
  * @throws HibernateException
  */
  public static Session currentSession() throws HibernateException {
  Session session = (Session) threadLocal.get();
  if (session == null) {
  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.openSession();
  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.
  */

[1] [2] 下一页

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训