⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hbsessionfactory.java

📁 达内网上购物系统
💻 JAVA
字号:
package org.whatisjava.dang.dao.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HbSessionFactory {
	private static Configuration conf;

	private static SessionFactory factory;

	private static ThreadLocal<Session> tl = new ThreadLocal<Session>();

	public synchronized static Session getSession() {
		Session session = tl.get();
		if (session == null) {
			if (factory == null) {
				if (conf == null) {
					conf = new Configuration();
					conf.configure();
				}
				factory = conf.buildSessionFactory();
			}
			session = factory.openSession();
			tl.set(session);
		}
		return session;
	}

	public static void closeSession() {
		Session session = tl.get();
		if (session != null) {
			session.close();
			tl.set(null);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -