venkat
venkat

Reputation: 31

How to stop reload the data from db for every request?

I loaded the data from db and set to beans. It diplay in screen. Suppose if i click any button in same screen, the data loaded again from db. All beans are null for every request. That is the reason i am unable to stop the loading. Can you please tell me how to resolve this issue?

I wrote code like this

public class Bean {

private Service service;

private String email;

private String name;

public void setService(final Service newService) {
if(this.service == null)
{
this.service = newService;
try {       
//load data from db set the values of email and name                          
 this.service.createContact(this);
} catch (SQLException e) 
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

facess-config.xml

<managed-bean>
<description>The bean to manage service.</description>
<managed-bean-name>Bean</managed-bean-name>
<managed-bean-class>com.bean.Bean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>Service</property-name>
<value>#{service}</value>
</managed-property>
</managed-bean>

Upvotes: 0

Views: 516

Answers (1)

Michael Borgwardt
Michael Borgwardt

Reputation: 346300

Your bean is declared with request scope. That means, it gets created new for each request. Quite apart from that: if(this.service == null) will always be true because the setter gets called only once when the bean is created (no matter what scope it has).

If you're sure the data will never change between requests, simply declare the bean with session scope, and it will be created once per user session, or application scope to have it created once for the entire application.

Upvotes: 2

Related Questions