Logic repeating during page render

I have a page with a datatable, as far as I can tell, to reload the page after a commandLink is clicked, the logic is repeating.

Here is the BackingBean Logic

public List getAllRecords(){

try{

AccountDAO acctDAO = DAOFactory.getNewAccountInstance();

Iterator iter = acctDAO.findAll().iterator();

this.wrappedAccounts = ListManager.makeList();

while (iter.hasNext()){

this.wrappedAccounts.add(new WrappedAccount((AccountBean)iter.next()));

}

//return selectAll();

}catch (NullPointerException nps){

System.err.println(nps.toString());

}

System.out.println("***All Records***");

return this.wrappedAccounts;

}

public String toggleEdit(){

return"edit";

}

The dataTable is bound to the getAllRecords method of bean.

Here is the output from the server log.

2006-08-03 13:59:35,111 DEBUG [org.hibernate.impl.SessionFactoryObjectFactory] JNDI lookup: hibernate/SessionFactory

2006-08-03 13:59:35,111 DEBUG [org.hibernate.impl.SessionFactoryObjectFactory] lookup: uid=402882a50cd5674b010cd5674c6f0000

2006-08-03 13:59:35,111 DEBUG [org.hibernate.jdbc.JDBCContext] successfully registered Synchronization

2006-08-03 13:59:35,111 DEBUG [org.hibernate.impl.SessionImpl] opened session at timestamp: 4729370931654656

2006-08-03 13:59:35,111 DEBUG [org.hibernate.transaction.JTATransaction] Looking for UserTransaction under: UserTransaction

2006-08-03 13:59:35,111 DEBUG [org.hibernate.transaction.JTATransaction] Obtained UserTransaction

2006-08-03 13:59:35,111 DEBUG [org.hibernate.transaction.JTATransaction] begin

2006-08-03 13:59:35,111 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)

2006-08-03 13:59:35,111 DEBUG [org.hibernate.jdbc.ConnectionManager] opening JDBC connection

2006-08-03 13:59:35,111 DEBUG [org.hibernate.SQL] select accountbea0_.Acct_ID as Acct1_0_, accountbea0_.Acct_Num as Acct2_0_, accountbea0_.Acct_Name as Acct3_0_ from Budget.Account accountbea0_ order by accountbea0_.Acct_Num

2006-08-03 13:59:35,111 INFO [STDOUT] Hibernate: select accountbea0_.Acct_ID as Acct1_0_, accountbea0_.Acct_Num as Acct2_0_, accountbea0_.Acct_Name as Acct3_0_ from Budget.Account accountbea0_ order by accountbea0_.Acct_Num

2006-08-03 13:59:35,111 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to open ResultSet (open ResultSets: 0, globally: 0)

2006-08-03 13:59:35,111 DEBUG [org.hibernate.loader.Loader] result row: EntityKey[com.stlouiscity.budget.database.beans.AccountBean#164]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.loader.Loader] result row: EntityKey[com.stlouiscity.budget.database.beans.AccountBean#165]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.loader.Loader] result row: EntityKey[com.stlouiscity.budget.database.beans.AccountBean#166]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.loader.Loader] result row: EntityKey[com.stlouiscity.budget.database.beans.AccountBean#167]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.loader.Loader] result row: EntityKey[com.stlouiscity.budget.database.beans.AccountBean#168]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.loader.Loader] result row: EntityKey[com.stlouiscity.budget.database.beans.AccountBean#307]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to close ResultSet (open ResultSets: 1, globally: 1)

2006-08-03 13:59:35,111 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)

2006-08-03 13:59:35,111 DEBUG [org.hibernate.jdbc.ConnectionManager] aggressively releasing JDBC connection

2006-08-03 13:59:35,111 DEBUG [org.hibernate.jdbc.ConnectionManager] releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.engine.TwoPhaseLoad] resolving associations for [com.stlouiscity.budget.database.beans.AccountBean#164]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.engine.TwoPhaseLoad] done materializing entity [com.stlouiscity.budget.database.beans.AccountBean#164]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.engine.TwoPhaseLoad] resolving associations for [com.stlouiscity.budget.database.beans.AccountBean#165]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.engine.TwoPhaseLoad] done materializing entity [com.stlouiscity.budget.database.beans.AccountBean#165]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.engine.TwoPhaseLoad] resolving associations for [com.stlouiscity.budget.database.beans.AccountBean#166]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.engine.TwoPhaseLoad] done materializing entity [com.stlouiscity.budget.database.beans.AccountBean#166]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.engine.TwoPhaseLoad] resolving associations for [com.stlouiscity.budget.database.beans.AccountBean#167]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.engine.TwoPhaseLoad] done materializing entity [com.stlouiscity.budget.database.beans.AccountBean#167]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.engine.TwoPhaseLoad] resolving associations for [com.stlouiscity.budget.database.beans.AccountBean#168]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.engine.TwoPhaseLoad] done materializing entity [com.stlouiscity.budget.database.beans.AccountBean#168]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.engine.TwoPhaseLoad] resolving associations for [com.stlouiscity.budget.database.beans.AccountBean#307]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.engine.TwoPhaseLoad] done materializing entity [com.stlouiscity.budget.database.beans.AccountBean#307]

2006-08-03 13:59:35,111 DEBUG [org.hibernate.engine.StatefulPersistenceContext] initializing non-lazy collections

2006-08-03 13:59:35,111 INFO [STDOUT] ***All Records***

2006-08-03 13:59:35,126 DEBUG [org.hibernate.impl.SessionFactoryObjectFactory] JNDI lookup: hibernate/SessionFactory

2006-08-03 13:59:35,126 DEBUG [org.hibernate.impl.SessionFactoryObjectFactory] lookup: uid=402882a50cd5674b010cd5674c6f0000

2006-08-03 13:59:35,126 DEBUG [org.hibernate.jdbc.JDBCContext] successfully registered Synchronization

2006-08-03 13:59:35,126 DEBUG [org.hibernate.impl.SessionImpl] opened session at timestamp: 4729370931716096

2006-08-03 13:59:35,126 DEBUG [org.hibernate.transaction.JTATransaction] Looking for UserTransaction under: UserTransaction

2006-08-03 13:59:35,126 DEBUG [org.hibernate.transaction.JTATransaction] Obtained UserTransaction

2006-08-03 13:59:35,126 DEBUG [org.hibernate.transaction.JTATransaction] begin

2006-08-03 13:59:35,126 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)

2006-08-03 13:59:35,126 DEBUG [org.hibernate.jdbc.ConnectionManager] opening JDBC connection

2006-08-03 13:59:35,126 DEBUG [org.hibernate.SQL] select accountbea0_.Acct_ID as Acct1_0_, accountbea0_.Acct_Num as Acct2_0_, accountbea0_.Acct_Name as Acct3_0_ from Budget.Account accountbea0_ order by accountbea0_.Acct_Num

2006-08-03 13:59:35,126 INFO [STDOUT] Hibernate: select accountbea0_.Acct_ID as Acct1_0_, accountbea0_.Acct_Num as Acct2_0_, accountbea0_.Acct_Name as Acct3_0_ from Budget.Account accountbea0_ order by accountbea0_.Acct_Num

2006-08-03 13:59:35,142 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to open ResultSet (open ResultSets: 0, globally: 0)

2006-08-03 13:59:35,142 DEBUG [org.hibernate.loader.Loader] result row: EntityKey[com.stlouiscity.budget.database.beans.AccountBean#164]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.loader.Loader] result row: EntityKey[com.stlouiscity.budget.database.beans.AccountBean#165]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.loader.Loader] result row: EntityKey[com.stlouiscity.budget.database.beans.AccountBean#166]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.loader.Loader] result row: EntityKey[com.stlouiscity.budget.database.beans.AccountBean#167]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.loader.Loader] result row: EntityKey[com.stlouiscity.budget.database.beans.AccountBean#168]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.loader.Loader] result row: EntityKey[com.stlouiscity.budget.database.beans.AccountBean#307]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to close ResultSet (open ResultSets: 1, globally: 1)

2006-08-03 13:59:35,142 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)

2006-08-03 13:59:35,142 DEBUG [org.hibernate.jdbc.ConnectionManager] aggressively releasing JDBC connection

2006-08-03 13:59:35,142 DEBUG [org.hibernate.jdbc.ConnectionManager] releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.engine.TwoPhaseLoad] resolving associations for [com.stlouiscity.budget.database.beans.AccountBean#164]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.engine.TwoPhaseLoad] done materializing entity [com.stlouiscity.budget.database.beans.AccountBean#164]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.engine.TwoPhaseLoad] resolving associations for [com.stlouiscity.budget.database.beans.AccountBean#165]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.engine.TwoPhaseLoad] done materializing entity [com.stlouiscity.budget.database.beans.AccountBean#165]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.engine.TwoPhaseLoad] resolving associations for [com.stlouiscity.budget.database.beans.AccountBean#166]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.engine.TwoPhaseLoad] done materializing entity [com.stlouiscity.budget.database.beans.AccountBean#166]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.engine.TwoPhaseLoad] resolving associations for [com.stlouiscity.budget.database.beans.AccountBean#167]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.engine.TwoPhaseLoad] done materializing entity [com.stlouiscity.budget.database.beans.AccountBean#167]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.engine.TwoPhaseLoad] resolving associations for [com.stlouiscity.budget.database.beans.AccountBean#168]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.engine.TwoPhaseLoad] done materializing entity [com.stlouiscity.budget.database.beans.AccountBean#168]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.engine.TwoPhaseLoad] resolving associations for [com.stlouiscity.budget.database.beans.AccountBean#307]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.engine.TwoPhaseLoad] done materializing entity [com.stlouiscity.budget.database.beans.AccountBean#307]

2006-08-03 13:59:35,142 DEBUG [org.hibernate.engine.StatefulPersistenceContext] initializing non-lazy collections

2006-08-03 13:59:35,142 INFO [STDOUT] ***All Records***

As you can see the DAO logic is being performed twice between the button click and the page reloading. Any Idea why?

[11837 byte] By [unobriania] at [2007-10-3 2:02:05]
# 1
I have exactly the same issue, but no idea about the reason...Jan
ed.o.neilla at 2007-7-14 19:00:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

The JSF specification doesn't guarantee that the getters will be called only once, so you can't assume they will only be called once. They could be invoked during validation phase and render response phase.

According to MVC you should never perform any operations in property getters. You should perform this kind of operation in a bean constructor or when performing action.

amitteva at 2007-7-14 19:00:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Check the 6-step JSF lifecycle. This happens during update model (bean.setProperty(component.getValue), after converting and validation) and the render response (bean.getProperty, after invoking action).

So do:public List getAllRecords() {

if (FacesContext.getCurrentInstance().getRenderResponse()) {

// do your query stuff

}

return records;

}

BalusCa at 2007-7-14 19:00:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...