Richfaces不必要地攻击数据库

时间:2009-09-04 16:32:58

标签: java-ee richfaces toplink

我有一个简单的CRUD项目,我正在使用richfaces和toplink。我注意到有些页面的加载速度很慢,因此我将toplink的日志级别转为FINE。在加载我的页面时,该页面列出了rich:datatable中的所有课程对象,toplink似乎运行相同的所有查询9次。使用rich:datascroller移动到下一组50个项目或排序列时,它再次运行相同的select all查询18次。

这是数据表:

<rich:dataTable value="#{CourseController.courses}"
                        id="table"
                        var="dataTableItem"
                        rendered="#{CourseController.courses.rowCount>0}"
                        rows="50"
                        onRowMouseOut="this.style.backgroundColor='#{a4jSkin.tableBackgroundColor}'"
                        onRowMouseOver="this.style.backgroundColor='#dcdcdc'">
            <f:facet name="header">
                <h:outputText value="Courses"/>
            </f:facet> 
            <rich:column sortBy="#{dataTableItem.id}">  
                <f:facet name="header">  
                    <h:outputText  value="ID"/>  
                </f:facet>  
                <h:commandLink action="#{CourseController.viewCourse}">  
                    <h:outputText value="#{dataTableItem.id}" /> 
                    <f:param name="id" value="#{dataTableItem.id}" />  
                </h:commandLink>
            </rich:column> 
            <rich:column sortBy="#{dataTableItem.name}">  
                <f:facet name="header">  
                    <h:outputText  value="Name"/>  
                </f:facet>  
                <h:outputText value="#{dataTableItem.name}" />  
            </rich:column>        
            <rich:column sortBy="#{dataTableItem.owner}">  
                <f:facet name="header">  
                    <h:outputText  value="Owner"/>  
                </f:facet>  
                <h:outputText value="#{dataTableItem.owner}" />  
            </rich:column>
            <rich:column rendered="#{LoginController.inSystemAdminGroup || LoginController.inOperationsManagerGroup || LoginController.inLogisticsCoordinatorGroup}">  
                <f:facet name="header">  
                    <h:outputText  value=""/>  
                </f:facet>  
                <h:commandLink action="#{CourseController.editCourse}">  
                    <h:outputText value="Edit/Delete"/>  
                    <f:param name="id" value="#{dataTableItem.id}" />  
                </h:commandLink>  
            </rich:column>
            <f:facet name="footer">
                <rich:datascroller id="datascroller" />
            </f:facet>
        </rich:dataTable>

我的控制器中由表调用的函数:

public DataModel getCourses() {
        System.out.println("Im in getCourses.  Fun Fun Fun.");
        model = new ListDataModel(courseFacade.findAll());
        return model;
    }

我的bean中由getCourses()函数调用的函数:

public List<Course> findAll() {
        System.out.println("Selectin all the courses in the freaking database!!!!!!!!!!!!!!!!!!!!!!");
        Query q = em.createQuery("select object(o) from Course as o WHERE o.deleted = FALSE ORDER BY o.name");
        List<Course> courses = q.getResultList();
        return courses;
    }

在使用表格加载页面时我的控制台输出:

Im in getCourses.  Fun Fun Fun.
Selectin all the courses in the freaking database!!!!!!!!!!!!!!!!!!!!!!
SELECT ID, MODIFIEDBY, ACRONYM, EMSID, DELETED, REMINDEREMAILTEXT, DATEADDED, NUMDAYSREMINDER, DATEDELETED, ADHOC, DATEMODIFIED, ADDEDBY, OWNER, DELETEDBY, OPTLOCK, NAME FROM COURSE WHERE (DELETED = ?) ORDER BY NAME ASC
        bind => [false]
Im in getCourses.  Fun Fun Fun.
Selectin all the courses in the freaking database!!!!!!!!!!!!!!!!!!!!!!
SELECT ID, MODIFIEDBY, ACRONYM, EMSID, DELETED, REMINDEREMAILTEXT, DATEADDED, NUMDAYSREMINDER, DATEDELETED, ADHOC, DATEMODIFIED, ADDEDBY, OWNER, DELETEDBY, OPTLOCK, NAME FROM COURSE WHERE (DELETED = ?) ORDER BY NAME ASC
        bind => [false]
Im in getCourses.  Fun Fun Fun.
Selectin all the courses in the freaking database!!!!!!!!!!!!!!!!!!!!!!
SELECT ID, MODIFIEDBY, ACRONYM, EMSID, DELETED, REMINDEREMAILTEXT, DATEADDED, NUMDAYSREMINDER, DATEDELETED, ADHOC, DATEMODIFIED, ADDEDBY, OWNER, DELETEDBY, OPTLOCK, NAME FROM COURSE WHERE (DELETED = ?) ORDER BY NAME ASC
        bind => [false]
Im in getCourses.  Fun Fun Fun.
Selectin all the courses in the freaking database!!!!!!!!!!!!!!!!!!!!!!
SELECT ID, MODIFIEDBY, ACRONYM, EMSID, DELETED, REMINDEREMAILTEXT, DATEADDED, NUMDAYSREMINDER, DATEDELETED, ADHOC, DATEMODIFIED, ADDEDBY, OWNER, DELETEDBY, OPTLOCK, NAME FROM COURSE WHERE (DELETED = ?) ORDER BY NAME ASC
        bind => [false]
Im in getCourses.  Fun Fun Fun.
Selectin all the courses in the freaking database!!!!!!!!!!!!!!!!!!!!!!
SELECT ID, MODIFIEDBY, ACRONYM, EMSID, DELETED, REMINDEREMAILTEXT, DATEADDED, NUMDAYSREMINDER, DATEDELETED, ADHOC, DATEMODIFIED, ADDEDBY, OWNER, DELETEDBY, OPTLOCK, NAME FROM COURSE WHERE (DELETED = ?) ORDER BY NAME ASC
        bind => [false]
Im in getCourses.  Fun Fun Fun.
Selectin all the courses in the freaking database!!!!!!!!!!!!!!!!!!!!!!
SELECT ID, MODIFIEDBY, ACRONYM, EMSID, DELETED, REMINDEREMAILTEXT, DATEADDED, NUMDAYSREMINDER, DATEDELETED, ADHOC, DATEMODIFIED, ADDEDBY, OWNER, DELETEDBY, OPTLOCK, NAME FROM COURSE WHERE (DELETED = ?) ORDER BY NAME ASC
        bind => [false]
Im in getCourses.  Fun Fun Fun.
Selectin all the courses in the freaking database!!!!!!!!!!!!!!!!!!!!!!
SELECT ID, MODIFIEDBY, ACRONYM, EMSID, DELETED, REMINDEREMAILTEXT, DATEADDED, NUMDAYSREMINDER, DATEDELETED, ADHOC, DATEMODIFIED, ADDEDBY, OWNER, DELETEDBY, OPTLOCK, NAME FROM COURSE WHERE (DELETED = ?) ORDER BY NAME ASC
        bind => [false]
Im in getCourses.  Fun Fun Fun.
Selectin all the courses in the freaking database!!!!!!!!!!!!!!!!!!!!!!
SELECT ID, MODIFIEDBY, ACRONYM, EMSID, DELETED, REMINDEREMAILTEXT, DATEADDED, NUMDAYSREMINDER, DATEDELETED, ADHOC, DATEMODIFIED, ADDEDBY, OWNER, DELETEDBY, OPTLOCK, NAME FROM COURSE WHERE (DELETED = ?) ORDER BY NAME ASC
        bind => [false]
Im in getCourses.  Fun Fun Fun.
Selectin all the courses in the freaking database!!!!!!!!!!!!!!!!!!!!!!
SELECT ID, MODIFIEDBY, ACRONYM, EMSID, DELETED, REMINDEREMAILTEXT, DATEADDED, NUMDAYSREMINDER, DATEDELETED, ADHOC, DATEMODIFIED, ADDEDBY, OWNER, DELETEDBY, OPTLOCK, NAME FROM COURSE WHERE (DELETED = ?) ORDER BY NAME ASC
        bind => [false]

1 个答案:

答案 0 :(得分:2)

JSF规范并不保证每个生命周期请求只调用一次getter,不要把昂贵的代码放在getter中(即你在每次调用getCourses时都在进行查询并创建datamodel,JSF规范允许调用这个方法。正如框架所要求的那样)。