My question is about the best practice how to handle the resultset of a SQL statement in a JSF application? In most examples it is recommended to use a ViewScoped backing bean with a method like:
@ManagedBean
@ViewScoped
public class MyController implements Serializable {
...
public List<MyData> getResult() {
// select data from SQL database
....
}
And the JSF page looks like this:
<h:dataTable value="#{myController.result}" var="record">
...
</h:dataTable>
But from my tests I can see, that the method getResult() is called several times when the page is rendered. This means that my SQL Query is called several times. This is - I think - a bad design.
So I do store the result in a member variable:
@ManagedBean
@ViewScoped
public class MyController implements Serializable {
private List<MyData> data=null;
...
public List<MyData> getResult() {
if (data!=null)
return data;
else {
// select data from SQL database
....
data=....;
}
}
But with this approach my bean now stores the data on the server. And this wastes memory!
For this reason I use a RequestScoped DataController. With this approach the result dataset it not unnecessarily stored on the server after the page was rendered.
I cannot understand why most of the examples concerning JSF with h:datatables don't go up on these two very important aspects.
So my question is: What is the recommended design of a JSF Backing Bean to query data from a database?