代码如下:
public List<Forum> replyForum(String forumId) {
   log.info("");
   try {
String sql = "from Forum as f where f.parentId=:forumId order by createTime";
Query query = getHibernateTemplate().getSessionFactory().openSession().createQuery(sql.toString());
query.setString("forumId",forumId);
List<Forum> list = query.list();
if (list != null && list.size() > 0) {
   log.debug("");
           log.info("");
   return list;
} else {
   log.debug("");
   return null;
}
   } catch (DataAccessException e) {
log.error("[ForumDaoHibernate]replyForum发生错误!\n"
    + FormatExceptionStack.formatException(e));
return null;
   }
}
我想问的是getHibernateTemplate().getSessionFactory().openSession().createQuery(sql.toString());这中查询方式用的人多吗,对于大型的运营网站来说,这样的查询怎么样?好吗?是不是还有更好的办法,或者没有。再者getSessionFactory().openSession()到底要不要close,如果要关闭session的话,该怎么关闭??新手急,在线等待答案,谢谢!!!!~~~~(>_<)~~~~ 

解决方案 »

  1.   

    getHibernateTemplate().getSessionFactory().openSession().createQuery(sql.toString());
    =====================================================================================
    这条语句里我不知道你指的是哪部分的用法,getHibernateTemplate().getSessionFactory()获得SessionFactory。
    SessionFactory是线程安全的,hibernate只会维护一个SessionFactory,所以你每次获得的都是同一个SessionFactory,
    这样用不会造成额外的资源消耗,也就是不会重新创建一个SessionFactory。
    session 不是线程安全的,所以用完后要关闭,调用session.close();就OK了
      

  2.   

    getHibernateTemplate().getSessionFactory().openSession().createQuery(sql.toString());可以这样使用
    用完了当然要关闭了啊
    SessionFactory sessions = new Configuration().configure().buildSessionFactory();
            Session session = sessions.openSession();
            Query query = session.createQuery("from Student");
            List<?> list = query.list();
            
            Iterator<?> it = list.iterator();
            while(it.hasNext())
            {
             Student stu = (Student)it.next();
             log.info("id: "+stu.getId()+" 姓名: "+stu.getSname()+" 学号: "+stu.getSno());
            
            }   
            sessions.close();
    看看这个
      

  3.   

    明白了,谢谢!但是,我还有一个问题就是:我现在开发一个运营型的网站,可是我的在网站上连续点上十几下就会挂掉,而且出现Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool exhausted或者Caused by: java.util.NoSuchElementException: Timeout waiting for idle object之类的错误,是不是跟hibernate配置的数据库连接池有关,只要调大一下maxActive的值就可以了吗?还有什么别的不安全因素,望指教!!
      

  4.   

    你这个新手还真新啊,你看下人家写的代码的功能和你那不差不多吗,其实数据库连接基本都要关闭的,还有就是异常捕获的.至于你的程序连接数目的最大限制,你自己都找到问题所在,试下也就可以了.还有提出你一个小错误,String.toString(),有意义吗,在代码的检测中,是通不过的.
      

  5.   

    多谢指教,我想问一下getHibernateTemplate().getSessionFactory().openSession()该怎么关啊,不会,嘿嘿!
      

  6.   

    getHibernateTemplate().getSessionFactory().openSession().close;
    这样就关了
      

  7.   

    晕,我也这样试过,可是还有部分人说getHibernateTemplate().getSessionFactory().openSession()根本不需要自己关,到底是怎么回事啊,我都搞糊涂了,越问越糊涂。
      

  8.   

    getHibernateTemplate 该是自己实现了,不用你关
      

  9.   


    private static String ZL_HBM_XML = "/ZLSearch.hbm.xml";
    SessionFactory sf = new Configuration().configure(ZL_HBM_XML).buildSessionFactory();  
    String queryString = "select SITE_NAME,CH_NAME from ODS_SITE@ODSCENTER order by CH_NAME";
    Query queryObject = sf.openSession().createSQLQuery(queryString);
    sf.close();            
            
    与系统默认的SessionFactory 相比是线程不安全的,要想办法塞到线程里面处理一下
    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    楼主给点分啊,穷。