public class HibernateUtil {
private static Log log=LogFactory.getLog(HibernateUtil.class);
private static final SessionFactory sessionFactory;
static {
try{
sessionFactory=new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("Initial SessionFactory creation failed.",ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session=new ThreadLocal();
//得到session
public static Session Session() throws HibernateException {
Session s=(Session)session.get();
//Open a new Session, if this Thread has none yet
if (s==null) {
s=sessionFactory.openSession();
session.set(s);
}
return s;
}
//关闭session
public static void closeSession() throws HibernateException {
Session s=(Session) session.get();
session.set(null);
if (s !=null)
s.close();
}
}[@more@]作者: louis850324 发表日期: 2006-12-30 16:40 文章属性: 原创
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/8271432/viewspace-887887/
,如需转载,请注明出处,否则将追究法律责任。