0
votes

I have a question about design problem caused by JSF2 managed bean scope mechanism (request scope).

I am building a web app using tabview component and datatable component of primefaces4. Each tab has one datatable with data retrieved from managed bean with request scope, which uses JDBC to get data from database. Therefore, every time user clicks on a tab, the managed bean will retrieve data from database. This is how the whole architecture is designed. Simple enough.

The purpose to use @RequestScoped for each ManagedBean is that users want to see real time data from database when they choose each tab (each request). All other scopes do not apply. Problem is that all tabs database accesses logic are executed when user chooses one tab. I think that is becaused of Ajax call request, so that every @RequestScoped beans are called.

This causes 2 drawbacks: 1. bad performance. Users need to wait for 4-5 seconds to see the datatable appeared after all beans are executed. 2. too much useless access to database.

My solution is to use Spring scheduler to manage all database accesses in seperate threads from managed bean, and then add a data cache layer to interact with managed bean. Spring scheduler may be designed to access database every 10 minutes, poll data and put them into data cache. So that managed bean will access all data in memory. Then I would like to use Primefaces Push API to push data to client side, so user don't need to refresh browser.

Obviously it is not in real time, but solves 2 problems stated.

Is there any other better or simplified solution ?

Thanks!

-------solution-----------

  1. change ManagedBean to sessionScoped
  2. add tabviewListener
  3. add tabview class to handle tab switch
  4. in tabview class, use method in this post to get managed bean at runtime.
    JSF 2 : Accessing managed bean's instance in the phase listener object?
  5. in tabview page, use cache="true" to increase performance, and let tabViewListner to update the whole tabview component which will update datatables inside.
1

1 Answers

1
votes

You should change the beans be viewscoped and change the page logic to update the table upon a tab click, instead of throwing the whole bean away after each request. Look into PrimeFaces ajax requests and upon a tab click, call a method on the bean that retrieves fresh data that can then be referenced by the p:dataTable, etc.