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

查看:16
本文介绍了在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;
}

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

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?

推荐答案

你应该不要将业务/数据库逻辑放在getter中.它们旨在仅返回数据,而不是初始化/加载/填充 bean 的数据.您应该在 bean 的 @PostConstruct、action 方法或任何事件方法中执行业务/数据库逻辑,它们都只被调用一次,而不是在 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;
}

如果您确实由于某种特殊原因需要在 getter 中执行此操作,那么您需要引入延迟加载.

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天全站免登陆