views:

403

answers:

1

I have a simple CRUD project that I'm using richfaces and toplink for. I've noticed some of the pages are rather slow to load, so I turned the logging level for toplink to FINE. When loading my page that lists all of my course objects in a rich:datatable, toplink appears to be running the same select all query 9 times. When using the rich:datascroller to move to the next set of 50 items or sorting a column, it again runs the same select all query 18 more times.

Here is the datatable:

<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>

The function in my controller that's called by the table:

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

The function in my bean that get's called by the getCourses() function:

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;
    }

And my console output when loading the page with the table:

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]
+2  A: 

JSF spec doesn't guarantee that getters are called only once per lifecycle request, dont put expensive code in getters (i.e. you are doing the query and creating the datamodel on each call of getCourses, JSF spec allows calling this method as much as the framework wants per request).

shipmaster
Is there a best practice for displaying all of the items like I am attempting to do? Should I be keeping my objects in memory as well as storing them to the database? I'll have multiple users editing these items at the same time, so I need a way to keep everything in sync.
KevMo
I used to add an explicit 'refresh' button that hits the db until I started using Seam, now I use the seam event model and outjection.
shipmaster