标签:blog import figure UI factory final void 初始化 thread
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static SessionFactory sessionFactory; static { // 创建Configuration,该对象用于读取hibernate.cfg.xml,并完成初始化 Configuration config = new Configuration().configure(); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(config.getProperties()); StandardServiceRegistry ssr = ssrb.build(); sessionFactory = config.buildSessionFactory(ssr); } /** * 获取SessionFactory * * @return */ public static SessionFactory getSessionFactory() { return sessionFactory; } public static final ThreadLocal<Session> session = new ThreadLocal<Session>(); public static Session getCurrentSession() { Session s = (Session) session.get(); if (s == null || !s.isOpen()) { s = sessionFactory.openSession(); session.set(s); } return s; } public static void closeSession(Session session) { if (null != session) { session.close(); } } }
标签:blog import figure UI factory final void 初始化 thread
原文地址:http://www.cnblogs.com/hello-daocaoren/p/6658197.html