Session封装的线程同步

<strong>package com.wfc.view;

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

public class HibernateUtil {
	private static SessionFactory sessionFactory=null;
	//使用线程局部模式
	private static ThreadLocal<Session> threadLocal=new ThreadLocal();
	private HibernateUtil(){};
	static {
		sessionFactory=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
	}
			//获取全新的session
			public static Session openSession(){
				return sessionFactory.openSession();
			}
			//获取线程相关联的Session
			public static Session getCurrentSession(){
				Session session=threadLocal。get() ;
					//判断是否得到
					if(session==null){
						session=sessionFactory.openSession();
						//把session放置到ThreadLocal,相当于该session已经和线程相关了
						threadLocal.set(session);
			}
					return session;
	}
	

}
</strong>

你可能感兴趣的:(Session封装的线程同步)