[java] view plain copy print ?
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- public class SessionUtil {
- private static SessionFactory factory;
- private static ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
- // 获取SessionFactory
- static { // 静态初始化块,加载配置信息
- Configuration cfg = new Configuration().configure(); // 读取hibernate.cfg.xml文件
- factory = cfg.buildSessionFactory(); // 建立SessionFactory
- }
- public static Session getSession() {
- Session session = threadLocal.get(); //获取当前线程下的session
- if(session==null) {
- System.out.println("session is null!");
- session = factory.openSession();
- threadLocal.set(session);
- }
- return session;
- }
- public static void closeSession() {
- Session session = threadLocal.get();
- //如果session不为null,则关闭session,并清空ThreadLocal
- if(session!=null) {
- session.close();
- threadLocal.set(null);
- }
- }
- }
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class SessionUtil {
private static SessionFactory factory;
private static ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
// 获取SessionFactory
static { // 静态初始化块,加载配置信息
Configuration cfg = new Configuration().configure(); // 读取hibernate.cfg.xml文件
factory = cfg.buildSessionFactory(); // 建立SessionFactory
}
public static Session getSession() {
Session session = threadLocal.get(); //获取当前线程下的session
if(session==null) {
System.out.println("session is null!");
session = factory.openSession();
threadLocal.set(session);
}
return session;
}
public static void closeSession() {
Session session = threadLocal.get();
//如果session不为null,则关闭session,并清空ThreadLocal
if(session!=null) {
session.close();
threadLocal.set(null);
}
}
}
接下来进行测试:
创建MyThread.java并继承Thread类
[java] view plain copy print ?
- import org.hibernate.Session;
- public class MyThread extends Thread{
- @Override
- public void run() {
- Session session1 = SessionUtil.getSession();
- Session session2 = SessionUtil.getSession();
- Session session3 = SessionUtil.getSession();
- long threadId = this.getId(); //获取线程id
- //输出hashCode,分辨是否是同一个session实例
- System.out.println("线程" +threadId+ ":\n" + session1.hashCode() + "\n" + session2.hashCode() + "\n" + session3.hashCode());
- }
- }
import org.hibernate.Session;
public class MyThread extends Thread{
@Override
public void run() {
Session session1 = SessionUtil.getSession();
Session session2 = SessionUtil.getSession();
Session session3 = SessionUtil.getSession();
long threadId = this.getId(); //获取线程id
//输出hashCode,分辨是否是同一个session实例
System.out.println("线程" +threadId+ ":\n" + session1.hashCode() + "\n" + session2.hashCode() + "\n" + session3.hashCode());
}
}
运行结果:
为什么使用TreadLocal来实现?
当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量的线程提供独立的变量副本,所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本。