1

I have method getStudents on managed BackBean, I am calling getstudents which inturn makes a call to database and fetches the data. The UI shows properly, but its causing performance issue as it takes too much time to load the page . Please suggest me how to handle this performance issue.

shreekanth
  • 459
  • 2
  • 12
  • 27
  • First check whether you are doing `only requried columns select query` or `selecting entire table` which will give you unnecessary data which effect page load time.This is one reason only.There may be other depennds on your code – SRy Mar 12 '13 at 05:06
  • @SrinivasR: I am calling required columns only.. In my case I m just getting student ids. This getstudent method is been used across various pages.. and My back end bean is session scoped. Each time this getter method is called it will make a call to DB which get list of students. – shreekanth Mar 12 '13 at 05:27
  • 1
    May be you don't have proper idea about bean scopes.In session scoped bean constructor will called only once.If you doing this database call in constructor the data which in the session will be loaded.It won't call the method again – SRy Mar 12 '13 at 05:30
  • Is it good practise to have a call to database inside a getter method? – shreekanth Mar 12 '13 at 05:34
  • 1
    No.It's not cause on every page load `getter` method be called. So, better load them inside a constructor of the `sessionbean` so, that it will be available entire session. – SRy Mar 12 '13 at 05:37
  • I have moved this to constructor, but after adding a new student, student list does not contain a new one, as per your previous comment since only once constructor called student list is not updating, After refreshing this will come again. so now what should I do change so that after adding it has to be reflected in studentList without refreshing?? – shreekanth Mar 12 '13 at 05:45
  • Initialize your list inside @PostConstruct as partlov has suggested. Also, if you're using a primefaces component that supports lazy-loading, use that to save memory... – Vrushank Mar 12 '13 at 08:30
  • Related: http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times/2090062#2090062 – BalusC Mar 13 '13 at 22:49

1 Answers1

3

You should no do business logic in getter methods. You should init your list in @PostConstruct method or do lazy loading in getter:

private List myList;

@PostConstruct
public void init() {
  // init my List
}

// getter and setter

@PostConstruct method will be called after managed bean instantiation. I suggest you to init in this method, not in constructor. As you are changing your list during the backing bean life, you should update it when it is changed. You can add data which was created by user, or you can chose to call database again after inserting values. You have to worry about this, there is no automation.

partlov
  • 13,789
  • 6
  • 63
  • 82