小菜鸟最近在学习EJB2的时候,发现一个比较怪异的情况。麻烦各位大侠指点下!
我首先创建了一个无状态的session bean.然后在该bean里面得到hibernate session.
最后,我多次运行main()函数,结果发现,在step2处,除了第一次打印出来是null以外,其它的都是有值的。
按照道理,不同线程访问,在step2处打印出来的总是null才对?public static void main(String[] args){
    EJBFactory factory = FactoryBean.getEJBFactory();
    ReportDataAccess reportData = factory.getReportDataAccess();
    reportData.doOperate();
}
......
public doOperate(){
    Session session = currentSession();
    ......
}
public static Session currentSession() {
    Session session = (Session) hibernateHolder.get();
    System.out.println(Thread.currentThread()+"========11========");    //step1
    System.out.println(session+"================");                     //step2
    if (session == null) {
      try {
        session = sessionFactory.openSession();
        txHolder.set(session.beginTransaction());
        hibernateHolder.set(session);
      }
      catch (HibernateException ex) {
        log.fatal("Factory open session failed", ex);
        throw new SessionInitialException("Factory open session failed", ex);
      }
    }
    return session;
  }