Problem using Stateful EJB in ManagedBean using RequestScope

I am using JSF 2.0 and EJB 3.1 on Glassfish v3 application server. And I ran into the actual problem:
In MenagedBean with RequestScope, I want to access a session object (EJB with @Stateful), which should store some information related to the session, like a selected category, a highlighted page (with a date binding for each category), etc. - nothing special, I think.
The first time you select a category, a data type is created and displayed. Everything is good. Now, if an element (row) is clicked (to show the details of the element) or if the next page is to be displayed, the session (EJB state) is recreated, and again the default values ​​are used to display and display the page.

The code looks like this:

@ManagedBean @RequestScoped public class TableViewBean { @EJB SessionBean session; public DataModel getTable() { return session.getDataModel(); } public SessionBean getSession(){ return session; } public void next() { session.getPaginator().nextPage(); session.resetList(); } public void previous() { session.getPaginator().previousPage(); session.resetList(); } // some other code } 

and EJB session:

 @Stateful public class SessionBean { private String selectedType = "Entity"; private DataModel dataModel; private int rowsPerPage = 5; private Paginator paginator; public void setSelectedType(String type){ if(!type.equalsIgnoreCase(selectedType)){ selectedType = type; updateService(); } resetList(); } public void resetList() { dataModel = null; } public void resetPagination() { paginator = null; } public int getRowsPerPage() { return rowsPerPage; } public void setRowsPerPage(int rowsPerPage) { this.rowsPerPage = rowsPerPage; resetList(); resetPagination(); } public Paginator getPaginator() { if (paginator == null) { paginator = new Paginator(rowsPerPage) { @Override public int getItemsCount() { return selectedService.getCount(); } @Override public DataModel createPageDataModel() { DataModel model = null; if(selectedService != null){ model = new ListDataModel(....); } return model; } }; } return paginator; } public DataModel getDataModel() { if(dataModel == null){ dataModel = getPaginator().createPageDataModel(); } return dataModel; } 

}

If I change the scope of the ManagedBean to SessionScope, everything will be fine, but I do not like it because of the use of memory problems.

What's wrong with my code ... please help me.

Greetz, Jerry

+4
source share
1 answer

Your RequestScoped ManagedBean is re-created for each request (which, after all, means RequestScoped). Therefore, with each instance, it receives the encapsulation of the new SFSB.

+5
source

Source: https://habr.com/ru/post/1333792/


All Articles