• Post Reply Bookmark Topic Watch Topic
  • New Topic

Logic repeating during page render  RSS feed

 
Brian Smith
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



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?
 
Dhananjay Inamdar
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brian,

As per going with Lifecycle of JSF, it is not sure that get/set method will get call only once. These methods will get called more than once in different phases of lifecycle.

So recommended practise is not to write any logic in get/set method. If you are writting some logic to poulate list from DAO then do following thing


public List getSomethingMethod(){
List methodLevelVariable;
if(methodLevelVariable == null){
methodLevelVariable = DAO.method();
}
return methodLevelVariable;
}

This coding will also get called more than once, but DAO/ database call will not happen for more than once.

I hope this will solve your problem.

Thanks
Dhananjay
 
Dhananjay Inamdar
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

In my sample code, just define 'list methodLevelVariable' as a global variable of class and not method level.

Thanks
Dhananjay
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!