1:getCurrentSession会把Session和当前的线程关联起来,而openSession只是重新开启一个Session
2:getCurrentSession获得的Session会在事务关闭或者回滚时会自动关闭,而openSession获得的Session必须手动关闭
getCurrentSession,特定的实现用它来负责跟踪当前的上下文session,Hibernate内置了此接口的两种实现
* org.hibernate.context.JTASessionContext --当前session通过当前执行的线程来跟踪和界定
* org.hibernate.context.ThreadLocalSessionContext --当前线程通过当前执行的线程和跟踪和界定
这两种实现都提供了“每数据库事务对应一个session”的编程模型,也称作每次请求一个session,Hibernate session的起始和终结由数据库事务的生存来控制。
3:关于ThreadLocal的理解
线程局部变量,就是为每一个使用某变量的线程都提供一个该变量值的副本,是每一个线程都可以独立的改变自己的副本,而不会和其他线程的副本冲突,从线程的
角度看就好像每一个线程都完全拥有一个变量
实例代码:
- package com.capinfotech.ju.util;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- public class HibernateUtil {
- public static final ThreadLocal sessionThreadLoacl = new ThreadLocal();
- public static final SessionFactory sessionFactory;
- static {
- try {
- sessionFactory = new Configuration().configure().buildSessionFactory();
- } catch(Throwable t) {
- throw new ExceptionInInitializerError(t);
- }
- }
- /**
- * 获取当前的Session
- * @return
- * @throws HibernateException
- */
- public static Session currentSession() throws HibernateException {
- Session session = (Session) sessionThreadLoacl.get();
- if(session == null) {
- session = sessionFactory.openSession();
- sessionThreadLoacl.set(session);
- }
- return session;
- }
- /**
- * 释放Session
- * @throws HibernateException
- */
- public static void closeSession() throws HibernateException {
- Session session = (Session)sessionThreadLoacl.get();
- if(session != null) {
- session.close();
- }
- sessionThreadLoacl.set(null);
- }
- }
在sessionFactory启动的时候,Hibernate会根据配置创建相应的CurrentSessionContext,在getCurrentSession()被调用的时候,实际被执行的方法是CurrentSessionContext.currentSession(),在currentSession()执行时,如果当前Session为空,currentSession会调用SessionFactory的openSession,
这样既保证了Session的线程安全,又不用每次数据库操作都重新获取一个Session实例,实现了Session重用