在dataTable的getter方法中调用数据库时出现性能问题,多次调用getter [英] Performance issues when calling database in getter method for dataTable, getter is called multiple times

查看:140
本文介绍了在dataTable的getter方法中调用数据库时出现性能问题,多次调用getter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JSF 2.0和Primefaces的新手,但已决定在看到primefaces展示时决定使用它们创建我的应用程序.

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

我已经完成了申请,但是注意到它非常慢.我在各个位置放置了一些system.out.println来查看正在调用的内容,并且我注意到有时在一个事件中,控制器中的方法(例如调用DAO来从数据库中检索值的方法)最多被调用6次. !我的页面中有很多数据表,因此有时在每个表中填充的每个表都有多个数据表* 6调用,这似乎是造成速度缓慢的原因.

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

在视图中,我将像在datatable元素上一样调用此方法以显示结果.

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

当我第一次加载它时,它只会调用一次,但是当我单击一个按钮以打开一个包含完全不相关数据的对话框时,此getAddresses()可能会被调用3-6次,并且与数据无关.我在目前的行动中要求.有谁熟悉此方法,以及如何提高我的应用程序速度?

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?

推荐答案

您应该将业务/数据库逻辑放入 getters 中.它们旨在仅返回数据,而不初始化/加载/填充bean的数据.您应该在bean的@PostConstruct,操作方法或任何事件方法中进行业务/数据库逻辑,这些逻辑仅被调用一次,而不是在getter中被调用.

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.

这篇关于在dataTable的getter方法中调用数据库时出现性能问题,多次调用getter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆