双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

学习hibernate 谁能给我解释一段下面的代码 很基础

ziseshatan ziseshatan 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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();

public static Session currentSession() 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;
}

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

}
尤其是下面这段------------------------------------------------
static {
try{
sessionFactory=new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("Initial SessionFactory creation failed.",ex);
throw new ExceptionInInitializerError(ex);
}
}
加上了个static 是什么意思 有什么用 所谓的静态代码 在什么情况下执行啊
...全文
评论 4
收藏 已收藏
分享
返回顶部
ziseshatan
Web 开发 81,108 成员 341,720 内容