每当我们访问一次服务器,它就会调用一个线程来响应我们的请求。当我第一次访问的时候它会调用一个线程,产生一个session,我再发送另一个请求的时候,session没有变,那现在的线程应该不是第一次访问的那个线程了吧?

解决方案 »

  1.   

    不是的了,session和线程没有关系
      

  2.   

    哦,那hibernate里面的,sessionfactory中的getSession()方法,将当前线程与session绑定,只在同一次访问中有效了。
      

  3.   

    hibernate session和http session也没有关系。
    楼主概念不清啊。
      

  4.   

    我清楚的,我的意思是,既然hibernatesession 与 线程绑定只在一次访问中有效的话, 为什么我们不把hibernatesession与用户会话的session绑定起来呢?
      

  5.   

    那根本是2码事情,http session是依赖cookie,hibernate session是跟thread。
    一个是在browser里保存,一个是thread结束就没有了。
    知道为什么说你概念不清了吧?
      

  6.   


    public class HibernateSessionFactory {    /** 
         * Location of hibernate.cfg.xml file.
         * Location should be on the classpath as Hibernate uses  
         * #resourceAsStream style lookup for its configuration file. 
         * The default classpath location of the hibernate config file is 
         * in the default package. Use #setConfigFile() to update 
         * the location of the configuration file for the current session.   
         */
        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
        //private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
        private static final Map<String,HttpSession> map= new HashMap<String,HttpSession>();
        private  static Configuration configuration = new Configuration();
        private static org.hibernate.SessionFactory sessionFactory;
        private static String configFile = CONFIG_FILE_LOCATION;    private HibernateSessionFactory() {
        }

    /**
         * Returns the ThreadLocal Session instance.  Lazy initialize
         * the <code>SessionFactory</code> if needed.
         *
         *  @return Session
         *  @throws HibernateException
         */
        public static Session getSession(String sessionid) throws HibernateException {
            Session session = (Session) map.get(sessionid); if (session == null || !session.isOpen()) {
    if (sessionFactory == null) {
    rebuildSessionFactory();
    }
    session = (sessionFactory != null) ? sessionFactory.openSession()
    : null;
    map.put(sessionid,session);
    }        return session;
        } /**
         *  Rebuild hibernate session factory
         *
         */
    public static void rebuildSessionFactory() {
    try {
    configuration.configure(configFile);
    sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
    System.err
    .println("%%%% Error Creating SessionFactory %%%%");
    e.printStackTrace();
    }
    } /**
         *  Close the single hibernate session instance.
         *
         *  @throws HibernateException
         */
        public static void closeSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
            threadLocal.set(null);        if (session != null) {
                session.close();
            }
        } /**
         *  return session factory
         *
         */
    public static org.hibernate.SessionFactory getSessionFactory() {
    return sessionFactory;
    } /**
         *  return session factory
         *
         * session factory will be rebuilded in the next call
         */
    public static void setConfigFile(String configFile) {
    HibernateSessionFactory.configFile = configFile;
    sessionFactory = null;
    } /**
         *  return hibernate configuration
         *
         */
    public static Configuration getConfiguration() {
    return configuration;
    }}我这不是绑定起来了?
      

  7.   

    httpsession 本质上是由服务器控制它的生命周期的,cookie只是为了找到对应的session而已。
      

  8.   

    LZ的想法是对的
    SESSION是保存在服务器端的,除非整个自己的结中SESSION或超时,它会一直存在。
    线程是由操作系统分配的,每次提交时都会生成一个新的线程。
      

  9.   

    又是微软与甲骨文之争.归纳一下,session同用户帐号密码相关.thread是同一个session下的一个task相关.但它们都有自己的粒度,比如说操作系统的session同browser下的session不是一个级别的.不对的地方欢迎指正.