程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2SE >> J2SE綜合:關於private構造函數(1)

J2SE綜合:關於private構造函數(1)

編輯:J2SE

看下面的類:
 
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.
 */
 private HibernateSessionFactory() {
 }
 }

本文來自編程入門網:http://www.bianceng.cn/Programming/Java/201107/27860.htm

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved