1

I am trying to load a datatable with pagination and sorting. Using the ExtendedDataModel and Arrangeable to achieve this and it is working fine. Then I added the Phase Listener to check the behavior and noticed that that

1. Pagination is working fine with only one DB Hit.
2. But while Sorting DB Hit happens twice, First at Phase 2 and Second at Phase 6.

Also note that if I am not having immediate="true" then the DB HIT Happens 4 times each in Phase 2, 3, 4 and 6.

Am not sure why this is happening. Can you please help on this.

xhtml snippet

                <rich:column sortBy="#{item.busName}" sortOrder="#{sortBean.order}">
                    <f:facet name="header">
                        <a4j:commandLink value="BUSINESS NAME" action="#{sortBean.sortByBus}" immediate="true"
                             render="riskData" />
                    </f:facet>
                    <h:outputText value="#{item.busName}">
                    </h:outputText>
                </rich:column>

                <rich:column sortBy="#{item.regionName}" sortOrder="#{sortBean.orderRegion}">
                    <f:facet name="header">
                        <a4j:commandLink value="REGION NAME"
                             action="#{sortBean.sortByRegion}"  immediate="true"
                             render="riskData" />
                    </f:facet>
                    <h:outputText value="#{item.regionName}">
                    </h:outputText>
                </rich:column>

----------PHASE LISTENER LOGS WITH SYSOUTS----------

Page Inital View with Unsorted Value having only one DB HIT to fetch the data ###

START PHASE RESTORE_VIEW 1
END PHASE RESTORE_VIEW 1
START PHASE RENDER_RESPONSE 6
Get Sort -> Order business -> unsorted
Get Sort -> Order region -> unsorted
Get Sort -> Order PolicyDt -> unsorted
Arrange Not Available
Total Count DB HIT
Data Creation
Get Sort -> Order business -> unsorted
Get Sort -> Order region -> unsorted
Get Sort -> Order PolicyDt -> unsorted
Arrange Not Available
Walk -> Start
DB HIT
First -> 0Last -> 3
Before Sort -> [com.entity.BusItem@514cef, com.entity.BusItem@1921eac, com.entity.BusItem@1bcdc8]
Before SortList
After Sort -> [com.entity.BusItem@514cef, com.entity.BusItem@1921eac, com.entity.BusItem@1bcdc8]
Walk -> End
END PHASE RENDER_RESPONSE 6


BusinessName Unsorted Ascending Sort having two DB HIT -> 1st in Phase2 and 2nd in Phase 6 ###

START PHASE RESTORE_VIEW 1
END PHASE RESTORE_VIEW 1
START PHASE APPLY_REQUEST_VALUES 2
Get Sort -> Order business -> unsorted
Get Sort -> Order region -> unsorted
Get Sort -> Order PolicyDt -> unsorted
Arrange Not Available
Walk -> Start
DB HIT
First -> 0Last -> 3
Before Sort -> [com.entity.BusItem@514cef, com.entity.BusItem@1921eac, com.entity.BusItem@1bcdc8]
Before SortList
After Sort -> [com.entity.BusItem@514cef, com.entity.BusItem@1921eac, com.entity.BusItem@1bcdc8]
Walk -> End
Sort Business -> Toggle -> Descending to Ascending
Set Sort -> Order business -> ascending
Set Sort -> Order PolicyDt -> unsorted
Set Sort -> Order region -> unsorted
END PHASE APPLY_REQUEST_VALUES 2
START PHASE RENDER_RESPONSE 6
Get Sort -> Order business -> ascending
Get Sort -> Order region -> unsorted
Get Sort -> Order PolicyDt -> unsorted
Arrange Available
/test.xhtml @53,74 sortBy="#{item.busName}"ascending
Get Sort -> Order business -> ascending
Get Sort -> Order region -> unsorted
Get Sort -> Order PolicyDt -> unsorted
Arrange Available
/test.xhtml @53,74 sortBy="#{item.busName}"ascending
Walk -> Start
DB HIT
First -> 0Last -> 3
Before Sort -> [com.entity.BusItem@514cef, com.entity.BusItem@1921eac, com.entity.BusItem@1bcdc8]
Before SortList
After Sort -> [com.entity.BusItem@1921eac, com.entity.BusItem@1bcdc8, com.entity.BusItem@514cef]
Walk -> End
END PHASE RENDER_RESPONSE 6

BusinessName Ascending to Descending Sort -> having two DB HIT 1st in Phase2 and 2nd in Phase 6 ###

START PHASE RESTORE_VIEW 1
END PHASE RESTORE_VIEW 1
START PHASE APPLY_REQUEST_VALUES 2
Get Sort -> Order business -> ascending
Get Sort -> Order region -> unsorted
Get Sort -> Order PolicyDt -> unsorted
Arrange Available
/test.xhtml @53,74 sortBy="#{item.busName}"ascending
Walk -> Start
DB HIT
First -> 0Last -> 3
Before Sort -> [com.entity.BusItem@1921eac, com.entity.BusItem@1bcdc8, com.entity.BusItem@514cef]
Before SortList
After Sort -> [com.entity.BusItem@1921eac, com.entity.BusItem@1bcdc8, com.entity.BusItem@514cef]
Walk -> End
Sort Business -> Toggle -> Ascending to Descending
Set Sort -> Order business -> descending
Set Sort -> Order PolicyDt -> unsorted
Set Sort -> Order region -> unsorted
END PHASE APPLY_REQUEST_VALUES 2
START PHASE RENDER_RESPONSE 6
Get Sort -> Order business -> descending
Get Sort -> Order region -> unsorted
Get Sort -> Order PolicyDt -> unsorted
Arrange Available
/test.xhtml @53,74 sortBy="#{item.busName}"descending
Get Sort -> Order business -> descending
Get Sort -> Order region -> unsorted
Get Sort -> Order PolicyDt -> unsorted
Arrange Available
/test.xhtml @53,74 sortBy="#{item.busName}"descending
Walk -> Start
DB HIT
First -> 0Last -> 3
Before Sort -> [com.entity.BusItem@1921eac, com.entity.BusItem@1bcdc8, com.entity.BusItem@514cef]
Before SortList
After Sort -> [com.entity.BusItem@514cef, com.entity.BusItem@1921eac, com.entity.BusItem@1bcdc8]
Walk -> End
END PHASE RENDER_RESPONSE 6

When Immediate is removed and then business name is sorted having fourDB HIT each in Phase2,3,4 and 6

START PHASE RESTORE_VIEW 1
END PHASE RESTORE_VIEW 1
START PHASE APPLY_REQUEST_VALUES 2
Get Sort -> Order business -> unsorted
Get Sort -> Order region -> unsorted
Get Sort -> Order PolicyDt -> unsorted
Arrange Not Available
Walk -> Start
DB HIT
First -> 0Last -> 3
Before Sort -> [com.entity.BusItem@514cef, com.entity.BusItem@1921eac, com.entity.BusItem@1bcdc8]
Before SortList
After Sort -> [com.entity.BusItem@514cef, com.entity.BusItem@1921eac, com.entity.BusItem@1bcdc8]
Walk -> End
END PHASE APPLY_REQUEST_VALUES 2
START PHASE PROCESS_VALIDATIONS 3
Walk -> Start
DB HIT
First -> 0Last -> 3
Before Sort -> [com.entity.BusItem@514cef, com.entity.BusItem@1921eac, com.entity.BusItem@1bcdc8]
Before SortList
After Sort -> [com.entity.BusItem@514cef, com.entity.BusItem@1921eac, com.entity.BusItem@1bcdc8]
Walk -> End
END PHASE PROCESS_VALIDATIONS 3
START PHASE UPDATE_MODEL_VALUES 4
Walk -> Start
DB HIT
First -> 0Last -> 3
Before Sort -> [com.entity.BusItem@514cef, com.entity.BusItem@1921eac, com.entity.BusItem@1bcdc8]
Before SortList
After Sort -> [com.entity.BusItem@514cef, com.entity.BusItem@1921eac, com.entity.BusItem@1bcdc8]
Walk -> End
END PHASE UPDATE_MODEL_VALUES 4
START PHASE INVOKE_APPLICATION 5
Sort Region-> Toggle -> Descending to Ascending
Set Sort -> Order region -> ascending
Set Sort -> Order business -> unsorted
Set Sort -> Order PolicyDt -> unsorted
END PHASE INVOKE_APPLICATION 5
START PHASE RENDER_RESPONSE 6
Get Sort -> Order business -> unsorted
Get Sort -> Order region -> ascending
Get Sort -> Order PolicyDt -> unsorted
Arrange Available
/footer.xhtml @62,83 sortBy="#{item.regionName}"ascending
Get Sort -> Order business -> unsorted
Get Sort -> Order region -> ascending
Get Sort -> Order PolicyDt -> unsorted
Arrange Available
/footer.xhtml @62,83 sortBy="#{item.regionName}"ascending
Walk -> Start
DB HIT
First -> 0Last -> 3
Before Sort -> [com.entity.BusItem@514cef, com.entity.BusItem@1921eac, com.entity.BusItem@1bcdc8]
Before SortList
After Sort -> [com.entity.BusItem@514cef, com.entity.BusItem@1921eac, com.entity.BusItem@1bcdc8]
Walk -> End

END PHASE RENDER_RESPONSE 6

msher420
  • 33
  • 3
  • 9

1 Answers1

1

Take a look at this issue.

Apparently this behavior is expected from the walk method. You are supposed to write an idempotent method, i.e., you should take appropriate action to query the DB only once.

But be aware that this method is called not only when paginating the results, but also when you call a method that takes a result as an argument, for example. What I did to solve the problem is to verify if the parameters for the query have changed, if so I execute the query and iterate through the results calling the visitor.process method on them, otherwise I just iterate through the current results.

Community
  • 1
  • 1
Thiago
  • 883
  • 11
  • 20