J2SE综合:关于private构造函数Java认证考试

文章作者 100test 发表时间 2009:04:10 00:45:33
来源 100Test.Com百考试题网


  看下面的类:
  HibernateSessionFactory.java
  package zy.pro.wd.util.
  import net.sf.hibernate.HibernateException.
  import net.sf.hibernate.Session.
  import net.sf.hibernate.cfg.Configuration.
  /**
  * Configures and provides access to Hibernate sessions, tied to the
  * current thread of execution. Follows the Thread Local Session
  * pattern, see {@link http://hibernate.org/42.html}.
  */
  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.
  */

相关文章


JAVA认证:Java中调用C_C 生成的DLLJava认证考试
JAVA认证:用J2SE5.0创建定制的泛型集合Java认证考试
J2SE综合:关于private构造函数Java认证考试
J2SE综合:属性配置文件的使用方法Java认证考试
java辅导:有关输入拼音自动提示汉字Java认证考试
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛