开发者

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

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 an开发者_开发技巧d 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:

  • Why JSF calls getters multiple times.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜