Stateful Session Beans Concept

Discussions

EJB programming & troubleshooting: Stateful Session Beans Concept

  1. Stateful Session Beans Concept (3 messages)

    When create() is called several times to the same Home object, it returns the same bean???????

    For example :

    The Stateful Session Bean MyBean has one private member named value:

    ... (Methods in MyBean)
    private int value;
    public int getValue() throws RemoteException;
    public void setValue(int v) throws RemoteException;

    In client application I create several MyBean instances on the same Home Object

    ... (Client Code)

    MyBean m1 = home.create();
    m1.setValue(5);
    System.out.println(m1.getValue()); // Prints 5
    MyBean m2 = home.crate();
    System.out.println(m2.getValue()); // Prints 5 !!!!

    Is create() returning the same instance???
    Isnt it supposed to return a brand new instance ???

    Thanks in advance,

    Daniel Melo
  2. Stateful Session Beans Concept[ Go to top ]

    The container will pick the same instance each time from the session bean pull when you call create on home. To get a new instance containing the specific state use create(...) methods.
  3. Hi! I am using websphere.
    I was trying to check the beahviour of stateless and stateful session (CMP) beans .
    The flow of the program is as follows:
    CASE 1(stateful session bean CMP)
    There is a main page which has a FORWARD(submit button)--------- >The control goes to servlet ----> The servlet gives a call to session bean object which contains a method called "Count".The Count method returns integer value.There is a variable called "counter" which is declared in the bean class. This counter is incremented every time we give a call to "count " method.

    OUTPUT:-

       Initially the value displayed is 0 --> click on back button --->goes to previous page---> click on forward button-------> value of counter now is 1 ---> the value of counter keeps on incrementeing .
         Now My question is :- if it is stateless session bean then every time I click on forward button ... the value of counter should remain 0.
         If I use STATEFUL BEEN then the value of"counter" remains 0. Why is that......

    CASE 2:
       The scenario is same as above. Only change is that now I am using httpsession and checking if a session exist or not?
        In this case two strange thigs are happening:
      a) value of "counter" is increasing regardless of stateless/stateful.

      b)Let's say when the value of counter =5, I now close the browser, and now I open a new browser and start fresh, but now when I click Forward button for the first time, it dispalys value of counter as "6"!!!!! and the process of incremention of counter continues.

        My question is shouldn't the session get destroyed as soon as I close the browser.

    I am attaching the code below....

    SERVLET CODE:
    ************************************************
    public class TestServlet extends HttpServlet implements Servlet {

    /**
    * @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
    */
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {

    }

    /**
    * @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
    */
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    String link=req.getParameter("num");
    System.out.println("hello");
    System.out.println(link);
    if("1".equals(link)){
    Context context;
    try {
    if(req.getSession(). getAttribute("test")==null){

    System.out.println("Database not empty");
    Context ic=new InitialContext();
    Object o = ic.lookup("ejb/com/mbt/beanTest/CounterHome");
    CounterHome home=(CounterHome) PortableRemoteObject.narrow(o,CounterHome.class);
    test = (Counter) home.create();
    int i=test.count();
    System.out.println("i->" + i);
    req.getSession().setAttribute("test",test);
    System.out.println("new session");
    }//end of session check if block
    else{
    test = (Counter)req.getSession().getAttribute("test");
    System.out.println("old session");
    int i=test.count();
    System.out.println("i->" + i);
    }
    }
    else{
    System.out.println("Database empty");
    }
    } catch (RemoteException e) {

    e.printStackTrace();
    } catch (CreateException e) {

    e.printStackTrace();
    }
    catch (NamingException e) {

    e.printStackTrace();
    }


    RequestDispatcher disp = this.getServletContext().getRequestDispatcher("test1.jsp");
    disp.forward(req, resp);
    }
    else if("2".equals(link)){
    RequestDispatcher disp = this.getServletContext().getRequestDispatcher("test2.jsp");
    disp.forward(req, resp);
    }
    }

    }

    *************************************************
    BEAN CODE:

    package com.mbt.beanTest;
    /**
     * Bean implementation class for Enterprise Bean: Counter1
     */
    public class Counter1Bean implements javax.ejb.SessionBean {
    private javax.ejb.SessionContext mySessionCtx;
    private int counter=0;
    /**
    * getSessionContext
    */
    public javax.ejb.SessionContext getSessionContext() {
    return mySessionCtx;
    }
    /**
    * setSessionContext
    */
    public void setSessionContext(javax.ejb.SessionContext ctx) {
    mySessionCtx = ctx;
    }
    /**
    * ejbCreate
    */
    public void ejbCreate() throws javax.ejb.CreateException {
    }
    /**
    * ejbActivate
    */
    public void ejbActivate() {
    }
    /**
    * ejbPassivate
    */
    public void ejbPassivate() {
    }
    /**
    * ejbRemove
    */
    public void ejbRemove() {
    }
    public int count(){
    return counter++;
    }
    }
  4. Stateful Session Bean is mainly used for maintaining session for a single client whereas Entity Beans is used for maintaining session between multiple clients. Maintaning State is nothing but the maintaining the value of variables between various calls. ie for various calls from a single client the value of the variable should be maintained. In your client your are using the same bean instance which in turn maintains the value of the variable.