title: Problem using Stateful EJB in ManagedBean with RequestScope
body: <p>I'm using JSF 2.0 and EJB 3.1 in the Glassfish v3 app server. And I'm facing actually the follwing problem:<br>
  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.<br>
  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.  </p>
  
  <p>The code looks like:</p>
  
  <pre><code>@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
      }
  </code></pre>
  
  <p>and the session EJB:</p>
  
  <pre><code>@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;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>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.</p>
  
  <p>What's wrong with my code...please help me.</p>
  
  <p>Greetz, Gerry</p>