hibernate5创建SessionFactory的详细工具类

 今天刚学习hibernate5做了一些笔记

我们要使用 Hibernate 的功能,首先需要读取 Hibernate 的配置文件,根据配置启动 Hibernate ,然后创建 SessionFactory。

Hibernate初始化类

Session对象是Hibernate中数据库持久化操作的核心,他负责Hibernate所有的持久化操作。那么如何获取Session对象呢?

Session对象是通过SessionFactory对象获取的,而SessionFactory又是通过Configuration对象来创建的。(代码如下)

hibernate5和hibernate4等版本有所不同。

创建HibernateUtil类,实现对Hibernate的初始化

package com.cszy.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {
	//为保证线程安全,将Seeeion放到ThreadLocal中管理。这样就避免了Session的多线程共享数据的问题
	private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private static SessionFactory sessionFactory = null;//SessionFactory对象
    //静态块(在类被加载时执行,且生命周期内只执行一次)
	static {
    	try {
    		// 加载Hibernate配置文件
			Configuration cfg = new Configuration().configure();
			//	创建会话工厂
			//	hibernate4.0版本前这样获取sessionFactory = configuration.buildSessionFactory();
			//	hibernate5以后规定,所有的配置或服务,要生效,必须配置或服务注册到一个服务注册类(服务构建器-->服务注册器)
			ServiceRegistry serviceRegistry = cfg.getStandardServiceRegistryBuilder().build();
			//  根据服务注册类创建一个元数据资源集,同时构建元数据并生成应用一般唯一的的session工厂
			sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
		} catch (Exception e) {
			System.err.println("创建会话工厂失败");
			e.printStackTrace();
		}
    }
	/**
     *	获取Session
     *  @return Session
     *  @throws HibernateException
     */
    public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();	//获取ThreadLocal中当前线程共享变量的值。
		if (session == null || !session.isOpen()) {
			if (sessionFactory == null) {		//如果会话工厂创建失败为空就在重新创建一次
				rebuildSessionFactory();
			}
			//创建Sqlsession数据库会话
			session = (sessionFactory != null) ? sessionFactory.openSession(): null;
			//设置ThreadLocal中当前线程共享变量的值。
			threadLocal.set(session);
		}

        return session;
    }
	/**
     * 重建会话工厂
     */
	public static void rebuildSessionFactory() {
    	try {
    		// 加载Hibernate配置文件
			Configuration cfg = new Configuration().configure();
			//	创建会话工厂
			//	hibernate4.0版本前这样获取sessionFactory = configuration.buildSessionFactory();
			//	hibernate5以后规定,所有的配置或服务,要生效,必须配置或服务注册到一个服务注册类(服务构建器-->服务注册器)
			ServiceRegistry serviceRegistry = cfg.getStandardServiceRegistryBuilder().build();
			//  根据服务注册类创建一个元数据资源集,同时构建元数据并生成应用一般唯一的的session工厂
			sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
		} catch (Exception e) {
			System.err.println("创建会话工厂失败");
			e.printStackTrace();
		}
	}
	/**
	 * 获取SessionFactory对象
	 * @return SessionFactory对象
	 */
	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	/** 
     *	关闭Session
     *  @throws HibernateException
     */
    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        //使用set(null)来回收ThreadLocal设置的值.
        threadLocal.set(null);
        if (session != null) {
            session.close();//关闭Session
        }
    }
}

上面的类中有用到ThredLocal

简单理解可以认为:ThreadLocal用于保存某个线程共享变量:对于同一个static ThreadLocal,不同线程只能从中get,set,remove自己的变量,而不会影响其他线程的变量。

1、ThreadLocal.get: 获取ThreadLocal中当前线程共享变量的值。

2、ThreadLocal.set: 设置ThreadLocal中当前线程共享变量的值。

3、ThreadLocal.remove: 移除ThreadLocal中当前线程共享变量的值。

ThreadLocal的set(null)和remove方法有什么区别?

set(null)把当前的ThreadLocal为key的值设为了空,避免线程下次再执行其他任务时被使用,但此时这个key对应的Entry值还在,只是Entry.value=null

remove方法会把这个key对应Entry的值设为空

 所以从重用和效率的角度来说,set(null)的性能优于remove,在实际的项目中推荐使用set(null)来回收ThreadLocal设置的值.

猜你喜欢

转载自blog.csdn.net/qq_39809458/article/details/81070636