I use Primafaces DataTable with scrolling pagination. I did everything, but the trouble loads very slowly ... From one page to another in 5 seconds even more there are only 20 records. I don’t know the reason for that

Here is my Bean

@Named(value = "maximaBean") @ViewScoped public class MaximaBean implements Serializable{ private List<Maxima> maximes = null; public List<Maxima> getMaximes() { MaximaDAO maximaDAO=new MaximaDAO(); maximes=maximaDAO.getMaximes(); return maximes; } } 

My DaoCLass

 public class MaximaDAO { private Session session = null; private List<Maxima> maximes=null; public List<Maxima> getMaximes() { session=HibernateUtil.getSessionFactory() .getCurrentSession(); Transaction tx = session.beginTransaction(); Query q = session.createQuery("From Maxima as maxima"); maximes=(List<Maxima>)q.list(); tx.commit(); tx=null; return maximes; } } 

and my xhtml

  <p:panel header="Example again" style="margin-bottom: 20px"> <h:form> <p:dataTable value="#{maximaBean.maximes}" var="maxim" rows="5" paginator="true" paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" rowsPerPageTemplate="3,5,8"> <p:column headerText="Id"> <h:outputText value="#{maxim.id}"></h:outputText> </p:column> </p:dataTable> </h:form> </p:panel> 

Already since yesterday I have been trying, the main thing in YouTube is that the boy has done everything for him and it doesn’t work for me

  • 3
    Just have a trip with YouTube accelerated shooting. - Sergey

2 answers 2

Most likely, the problem is not in JSF , but in working with the database. You have in the selection of the table does not specify the limit on the number of records. In this case, Hibernate reads all the records from the table, which can be a very long operation if there are tens / hundreds of thousands of records.

For the interface part with the database functions to work correctly, more complex data reading mechanisms should be developed, which should take the current pagination state from the dataTable and use them in the Query.setFirstResult and Query.setMaxResults .

For example, you can create a class that extends the org.primefaces.model.LazyDataModel and overrides its method

 public List<T> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters). 

Use the value of the first field for setFirstResult , and the pageSize field for setMaxResults . If you make the MaximaBean component extend the LazyDataModel , then in the dataTable it will be passed like this:

 p:dataTable value="#{maximaBean}" 

PS If only data is sampled without modification, then opening and then completing a transaction is not necessary, it is a waste of DBMS resources. And make sure that the received sessions are returned to the pool or closed - the code for releasing this resource is not visible.

    Take a closer look at your maximaBean, the outstanding list of maximes.
    Each time you access the list, the download is repeated from scratch. And the appeals, I tell you, are simply unmeasured. Virtually every field.

    When displaying the table, approximately this code is called.

     for (int currentIndex = offset; currentIndex < pageSize; currentIndex++) { ... maximaBean.getMaximes().get(currentIndex).getId(); ... } 

    It can be seen that the number of times the id is displayed, the entire table is loaded as many times.
    And you are lucky that the new list is not very different in composition. Although on the other hand the error would be more obvious.

    You can put a breakpoint on getMaximes and see how many times it will be called when processing the page.

    Therefore, you need to load only once. Something like this:

     public List<Maxima> getMaximes() { if (maximes == null) { MaximaDAO maximaDAO = new MaximaDAO(); maximes = maximaDAO.getMaximes(); } return maximes; } 

    No wonder that the private List<Maxima> maximes field has been private List<Maxima> maximes .

    First you need to learn it. And further, depending on the size of the table, move towards LazyDataModel, as recommended in another answer.

    • Thank you very much. I looked at the logs, you were right there so many requests go that you can get fucked up and after you started checking whether it’s full or not, everything changed and it became easier ... Thank you so much !!!! - elik