package com.lh.dao;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
 * 该类用于创建SessionFactory实例和Session实例
 * @author LH
 */
public class SessionFactoryProvider {
	
	//Hibernate配置文件的路径的变量
    private static String CONFIG_FILE = "/hibernate.cfg.xml";  
    // 创建一个线程本地变量
	private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
	//配置 Hibernate的类 ,根启动Hibernate,创建SessionFactory实例  
    private  static Configuration configuration = new Configuration();
    //声明私有类型的SessionFactory
    private static org.hibernate.SessionFactory sf;
	static {
    	try {
			configuration.configure(CONFIG_FILE);		//加载Hibernate配置文件
			sf = configuration.buildSessionFactory();	//根据配置文件创建SessionFactory实例
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    private SessionFactoryProvider() {
    }
    /**
     * 静态方法,用于获取Session实例
     * @return
     * @throws HibernateException
     */
    
    public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();	//获取此线程局部变量的当前线程副本中的session
		if (session == null || !session.isOpen()) {		//判断Session实例是否存在
			if (sf == null) {//判断SessionFactory实例是否为空,如果为空重建SessionFactory实例
				rebuildSessionFactory();
			}
			session = (sf != null) ? sf.openSession(): null;//从SessionFactory实例中获取Session实例
			threadLocal.set(session);//将此线程局部变量的当前线程副本中的值设置为session
		}
        return session;
    }
    /**
     * 静态方法,重建SessionFactory实例
     */
    
	public static void rebuildSessionFactory() {
		try {
			configuration.configure(CONFIG_FILE);
			sf = configuration.buildSessionFactory();
		} catch (Exception e) {	
			e.printStackTrace();
		}
	}
	/**
	 * 静态方法,关闭Session
	 * @throws HibernateException
	 */
	
    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);
        if (session != null) {
            session.close();
        }
    }
    /**
     * 获取SessionFactory实例
     * @return
     */
	public static SessionFactory getSessionFactory() {
		return sf;
	}
	/**
	 * 设置Hibernate的配置文件路径名
	 * @param configFile
	 */
	public static void setConfigFile(String configFile) {
		SessionFactoryProvider.CONFIG_FILE = configFile;
		sf = null;
	}
	/**
	 * 获取用于配置Hibernate的配置对象
	 * @return
	 */
	public static Configuration getConfiguration() {
		return configuration;
	}
	
}
最近下载更多
                
                过过成功尺寸     LV7
                2021年12月22日
            
            
        
                kiritoYCQ     LV5
                2020年11月28日
            
            
        
                dyihui     LV12
                2020年11月19日
            
            
        
                霖望1234     LV2
                2020年7月18日
            
            
        
                本机号码     LV2
                2020年7月6日
            
            
        
                秘制酒心巧克力     LV6
                2020年6月19日
            
            
        
                wangshangkun     LV2
                2020年5月25日
            
            
        
                木扬木     LV2
                2020年4月25日
            
            
        
                大杰打劫     LV8
                2020年3月24日
            
            
        
                gezongyuan     LV9
                2020年1月7日
            
            
        
最近浏览更多
                
                PISCESPLUS     LV5
                2024年9月3日
            
            
        
                ymrdhgv    
                2024年6月24日
            
            
                    暂无贡献等级
            
        
                晴曛     LV6
                2024年2月20日
            
            
        
                xiquyiyuan     LV10
                2024年1月12日
            
            
        
                WBelong     LV8
                2023年12月28日
            
            
        
                Henew168     LV2
                2023年12月4日
            
            
        
                李朝磊     LV18
                2023年10月23日
            
            
        
                陆程江     LV2
                2023年5月28日
            
            
        
                uni-code_0123     LV1
                2023年4月20日
            
            
        
                ssmss951     LV3
                2023年2月27日
            
            
        
                
                
                