5 Replies Latest reply on Apr 15, 2012 2:04 AM by iabughosh

    Getter of list variable pointing to data model for richTable is called unnecessarily often

    Georg Nozicka Newbie

      Imagine the following code fragments:

       

      <h:outputText id="name" value="#{testBean.name}" />

       

      <rich:dataTable var="var" value="#{testBean.names}" >

        <rich:column>

          <h:outputText value="#{var}" />

        </rich:column>

      </rich:dataTable>

       

      <a4j:commandButton id="an" value="Test Ajax" action="#{testBean.nameAction}" render="@this" execute="@this" />

       

      When the command button is clicked, the following happens:

      • Mehtod getNames is called
      • Action nameAction is called
      • Method getNames is called again

       

      I am wondering why getNames is called two times, I would have expected that it is called not at all as with the getName method resulting from the outputText. By the way, method getNames is also called if immediate is true for commandButton.

       

      In my opinion this is very impractical that the getter of the data model is called. Imagine the following scenario: what is shown in the table, is generated from a query in a data base. The idea is, that the query is only performed if a specific button (a search button) is clicked, but not for any other button or link residing on the same page so that the query is not unnecessarily called.

       

      Best Regards,

      Georg

        • 1. Re: Getter of list variable pointing to data model for richTable is called unnecessarily often
          iabughosh Master

          Hello Georg,

          bean getters and setters are called in JSF lifecycle, so it will be called more than you want to, accessing database in getters and setters are considered bad approach, and for search you can access the data base in search action and store the result in your list, take a look at this psudo code :

           

          public void searchActionListener(ActionEvent ae)  {

               //access your database

               List<YourBean> result = dao.getData();

               names.addAll(result);

          }

          • 2. Re: Getter of list variable pointing to data model for richTable is called unnecessarily often
            Georg Nozicka Newbie

            Hello Ibrahim,

             

            well, there are two things coming into my mind:

             

            1) Independently of your issue that a query should not be done in a getter (I will elaborate on this in my second point). I do not understand why with respect to my scenario getName is not called (what I would expect) and getNames is called (what I do not expect resp. do not understand). For commandButton I specified render=”@this” so I would expect (besides several other things) that the variable “names” in my bean is completely ignored (and therefore also getNames is not called). Let's look at this issue from another point of view: why is getName not called and why is getNames called? This looks kind of inconsequently to me respectively how is the rule which getter is called and which is not called? So for the moment this looks kind of try and error to me when and for which tags the getter is called and when not.

             

            2) Principally I agree that in a getter not too much functionality should be done. In your suggestion (and this is also what I have done anyways because I have no other idea how to to it) the data are retrieved when the action is called and then stored in a variable of the bean (in my case the variable “names”). What I do not like so much about this solution is, that potentially a lot of data are held in that list and this is done for every user who currently is connected to the server. So if each list holds lets say a couple of hundred (or thousand) entries and you have lets say a couple of thousand users connected at the same time to the server, then a lot of data are held in all these session beans and this uses up a lot of server memory. This is exactly what should not happen. So my idea was to do it in the following way which only works, if the getter would not be called when the commandButton is clicked.

             

            The data for the dataTable are only retrieved in getNames and are not held in any session bean, i.e. the getter would look more or less like this:

             

            public List<String> getNames() {

                 return dao.getData();

            }

             

            The trick would be, that the render  for the table is only specified for the commandButton which is responsible for a new search, any other button or link or what ever must not render the dataTable.

             

            At least this is the only solution which came into my mind. Maybe there are better one and I would be curious to get them know. So the real question is how to design the system so that the following two goals are satisfied:

             

            • A query is only done when the user explicitly issues it (e.g. by clicking a respective and dedicated button).
            • The data returned from the data base are not held in any session bean to save server memory.

             

            Best Regards,

            Georg

            • 3. Re: Getter of list variable pointing to data model for richTable is called unnecessarily often
              iabughosh Master

              about point number 1, could you replace h:outputText with h:inputText ? tell me what is the output.

              and for number 2, if you are using JPA/EJB you could use 2nd level cache to solve your problem.

               

              regards.

              • 4. Re: Getter of list variable pointing to data model for richTable is called unnecessarily often
                Georg Nozicka Newbie

                If it is an inputText, the get method is also called as like the get method of names. The rule obviously is, that methods of tags which represent some kind of input are called, for output only tags they are not called. Anyways, I would have assumed that dataTable is rather an output than an input tag, isn't it?

                 

                For my use case 2nd level cache is no option, because the query is done based on various input filters (search criteria), so for every use a different result list (based on the search criteria) is returned.

                 

                Regards,

                Georg

                • 5. Re: Getter of list variable pointing to data model for richTable is called unnecessarily often
                  iabughosh Master

                  i think dataTable is considered as input component because it allows input components inside its columns.

                  about your second case, i suggest you to make a bean attribute with setters & getters, filling your data when you click the search button, and use DB paging to navigate between large data, with this approach even if there is a large number of users every user will hand about 5->10 records at a time (which is not that much).

                   

                  regards.