Performance issues when calling database in getter method for dataTable, getter is called multiple times

StackOverflow https://stackoverflow.com/questions/4669651

  •  10-10-2019
  •  | 
  •  

문제

I am new to JSF 2.0 and Primefaces but have decided to create my application using them seeing the primefaces showcase.

I have completed my application but have noticed that it is extremely slow. I have placed some system.out.println in various places to see what is being called and I noticed that sometimes methods in my controller such as methods that call my DAO to retrieve values from the Database are being called up to 6 times on one event! My pages have a lot of datatables in them so sometimes multiple datatables * 6 calls for each list being populated in each table seems like this is what is causing the slowness.

I am not sure what I have done wrong or if I did anything wrong but in my controller for instance I have a method that might look like this,

public List<Addresses> getAddresses() {
     List<Addresses> addr = systemDao.getAddresses(userBean.userId);
     return addr;
}

in the view I will then call this method like on a datatable element to display the result.

When I first load it it will only call this once but when I click maybe a button to open a dialog with completely unrelated data this getAddresses() might be called 3 - 6 times and it has nothing to do with the data the I am requesting during the current action. Is anyone familiar with this and how I could maybe speed up my application?

도움이 되었습니까?

해결책

You should not put business/database logic in getters. They are intented to only return the data, not to initialize/load/populate the bean's data. You should be doing business/database logic inside bean's @PostConstruct, action method or any event methods, which are all invoked only once, not inside getters.

private List<Addresses> addr;

@PostConstruct
public void init() {
     addr = systemDao.getAddresses(userBean.userId);
}

public List<Addresses> getAddresses() {
     return addr;
}

If you really need to do it in the getter for some exotic reason, then you need to introduce lazy loading.

See also:

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top