Problem using Stateful EJB in ManagedBean with RequestScope

旧巷老猫 提交于 2019-12-13 13:33:19

问题


I'm using JSF 2.0 and EJB 3.1 in the Glassfish v3 app server. And I'm facing actually the follwing problem:
In a MenagedBean with RequestScope I want to access a session object (an EJB with @Stateful) which should store some session relevant information as the seleced category, the seleced page (with a datatable paginator for each category), etc. - nothing special I think.
The first time a category is selected, the datatable is created and displayed. Allright so far. Now, if an item (row) is clicked (to show the item's details) or if the next page should be displayed, the session (the stateful EJB) is recreated and again the default values are used to display and render the page.

The code looks like:

@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 the session EJB:

@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 works fine, but I don't like this, because of use of memory concerns.

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

Greetz, Gerry


回答1:


Your RequestScoped ManagedBean is re-instantiated for each request (that's what RequestScoped means after all). Therefore, with each instantiation it gets injected with a new SFSB instance.



来源:https://stackoverflow.com/questions/4562941/problem-using-stateful-ejb-in-managedbean-with-requestscope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!