关于hibernate session管理的一些问题
hibernate中,sessionFactory是线程安全的,可以被所有的应用程序共享,而session不是线程安全的,被多个线程共享时会出现不可预知的后果,那么对session 采用ThreadLocal实现共享会出问题吗?
public class HibernateUtil { private static SessionFactory sessionFactory; static{try{ sessionFactory = new Configuration().configure().buildSessionFactory();}catch(HibernateException e){ throw new RunTimeException("Configuration problem:"+e.getMessage());} } public static final TreadLocal session = new ThreadLocal(); public static Session currentSession() thrwos HibernateException{ Session s = (Session) session.get();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(null != s){ s.close();} }}