static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
以上是HibernateUtil.java的初始化hibernate的代码
主要是让hibernate去解析相关的xml文件;准备向外提供服务
hibernate工作的最小的单位是session;所以你看那些方法里首先都需要得到一个session;如
public static void add(Object obj) {
if (obj == null) {
return;
}
Session s = currentSession();
Transaction tx = s.beginTransaction();
s.save(obj);
tx.commit();
}
session会去生成sql并去操作数据库
其中Session s = currentSession();就是获取session;你可以进一步看看currentSession()的代码:
public static Session currentSession() {
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就会让sessionFactory.openSession();有的话就获取当前的session;Session s = (Session) session.get();
启动tomcat的时候静态化的代码会被执行;这个过程是自动的;不需要程序员调用
static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
这是一个静态的初始化器;jp里学过的;在jvm进程启动的时候执行且执行一次
注意HibernateUtil.java中有2种session:
1: (java.lang.)ThreadLocal session
2: (org.hibernate.)Session s
s才是hibernate里面的处理和数据库交互的session;你可以仔细研究下里面的各种方法
1: (java.lang.)ThreadLocal session 是ThreadLocal这个类的一个对象;是每一个具体线程的本地变量容器。触发的每一个action都会在tomcat启动一个线程;每一个线程在运行过程中自己使用的相关数据,对象,等等都可以存在它的ThreadLocal对象中。 每个线程都有自己的ThreadLocal对象,即session。
如某个action触发的线程会操作数据库,那么它会调用hibernate的s来做;这时 session(见1)里会存有s(见2)的。
因此: public static Session currentSession() {
Session s = (Session) session.get();//如果当前线程如果还活着, 就会通过这句话获得hibernate的Session s;
如果当前线程的ThreadLocal(session)里取出的Hibernate Session(s)是空;说明这个action是第一次来操作数据库,于是从初始化好的SessionFactory里拿一个
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
以上就是hibernate里开始过程
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
以上是HibernateUtil.java的初始化hibernate的代码
主要是让hibernate去解析相关的xml文件;准备向外提供服务
hibernate工作的最小的单位是session;所以你看那些方法里首先都需要得到一个session;如
public static void add(Object obj) {
if (obj == null) {
return;
}
Session s = currentSession();
Transaction tx = s.beginTransaction();
s.save(obj);
tx.commit();
}
session会去生成sql并去操作数据库
其中Session s = currentSession();就是获取session;你可以进一步看看currentSession()的代码:
public static Session currentSession() {
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就会让sessionFactory.openSession();有的话就获取当前的session;Session s = (Session) session.get();
启动tomcat的时候静态化的代码会被执行;这个过程是自动的;不需要程序员调用
static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
这是一个静态的初始化器;jp里学过的;在jvm进程启动的时候执行且执行一次
注意HibernateUtil.java中有2种session:
1: (java.lang.)ThreadLocal session
2: (org.hibernate.)Session s
s才是hibernate里面的处理和数据库交互的session;你可以仔细研究下里面的各种方法
1: (java.lang.)ThreadLocal session 是ThreadLocal这个类的一个对象;是每一个具体线程的本地变量容器。触发的每一个action都会在tomcat启动一个线程;每一个线程在运行过程中自己使用的相关数据,对象,等等都可以存在它的ThreadLocal对象中。 每个线程都有自己的ThreadLocal对象,即session。
如某个action触发的线程会操作数据库,那么它会调用hibernate的s来做;这时 session(见1)里会存有s(见2)的。
因此: public static Session currentSession() {
Session s = (Session) session.get();//如果当前线程如果还活着, 就会通过这句话获得hibernate的Session s;
如果当前线程的ThreadLocal(session)里取出的Hibernate Session(s)是空;说明这个action是第一次来操作数据库,于是从初始化好的SessionFactory里拿一个
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
以上就是hibernate里开始过程