SessionFactory是一个重量级组件,一个数据源应该只创建一个SessionFactory对象,而我需要使用Namingstrategy,方式如下:  
Configuration cfg = new Configuration();  
MyNamingStrategy m = new MyNamingStrategy(tableName);  
cfg.setNamingStrategy(m);  
sessionFactory = cfg.configure().buildSessionFactory();  
Session session = sessionFactory.openSession();  
其中,tableName是一个字符串变量,并不是一个固定不变的字符串。但这样一来,每次开启一个session就需要创建一个SessionFactory对象,这将大大占用资源,请问如何在使用命名策略的情况下不用开启大量的SessionFactory对象?

解决方案 »

  1.   

    刚在SQLserver版块发了这个帖子,可能位置不对,所以转到这里来了,请了解的帮帮我,谢谢!
      

  2.   

    SessionFactory 果断使用静态 。如果是多数据库,用静态 hash 里面加 静态 session faction
      

  3.   

    只有一个数据库,但里面有非常多的表,而且各个表只有表名不同,表里的字段是完全相同的,比如有表格table1,table2,table3...,table100...,因此在hibernate映射时,只是用了一个映射文件table.hbm.xml,在往相应的table表里存数据时,使用Configuration cfg = new Configuration(); MyNamingStrategy m = new MyNamingStrategy(tableName); cfg.setNamingStrategy(m); 通过使用命名策略,使用Configuration创建SessionFactory对象,并用此对象开启的session能够正确的操作需要的表格。
    但问题是,当同时处理大量用户的请求时,就必须创建大量的SessionFactory对象,耗费了大量系统资源,进而导致系统崩溃,请问这个怎么处理呢?  
      

  4.   

     /** 
         * 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 Configuration configuration = new Configuration();    
        private static org.hibernate.SessionFactory sessionFactory;
        private static String configFile = CONFIG_FILE_LOCATION; static {
         try {
    configuration.configure(configFile);
    sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
    System.err
    .println("%%%% Error Creating SessionFactory %%%%");
    e.printStackTrace();
    }
        }
        private HibernateSessionFactory() {
        }

    /**
         * Returns the ThreadLocal Session instance.  Lazy initialize
         * the <code>SessionFactory</code> if needed.
         *
         *  @return Session
         *  @throws HibernateException
         */
        public static Session getSession() throws HibernateException {
            Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) {
    if (sessionFactory == null) {
    rebuildSessionFactory();
    }
    session = (sessionFactory != null) ? sessionFactory.openSession()
    : null;
    threadLocal.set(session);
    }        return session;
        }
    这个是自动生成 hibernate 数据访问类,你看session = (sessionFactory != null) ? sessionFactory.openSession()
    这句,很明显一级缓存,映射那块,只需要加载一次,如果你没有完整的数据访问类,可以用myeclipse 自动生成。
      

  5.   

    问题是,我使用了命名策略,这样就必须创建大量的SessionFactory对象。你看下3楼我的回复,不是session缓存的问题,而是太多的重量级组件SessionFactory对象,只加载一次映射是不能解决动态查找相应的数据库表格的。