users@glassfish.java.net

Stateful Session Bean [One instance per client ? Really ?]

From: <glassfish_at_javadesktop.org>
Date: Tue, 11 Nov 2008 14:19:47 PST

Hi everyone.
I wrote a simple program to practice managing sessions using Stateful EJB.
I read: Each connected client gets an instance of the Stateful session been that is called.
But in my test, each time I refresh the page (simple F5) I get newest instance (tested with Object.hashcode())... I really don't understand how come

Here's some of my code:
My SessionBean Implementation:
[code]package helb.test.ejb;

import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.ejb.Stateful;


@Stateful(name="SessionManager", mappedName="ejb/SessionManagerJNDI")
public class SessionManagerImpl implements SessionManager, SessionManagerLocal {

    private ArrayList<String> users;
    
    @PostConstruct
    public void Initialize (){
        this.users = new ArrayList<String>();
        Integer i = this.hashCode();
        this.users.add("Hash: " + i.toString());
    }
    
    public void addUser (String user){
        this.users.add(user);
    }
    
    public void delUser (String user){
        this.users.remove(user);
    }
    
    public String findUser (String user){
            String res = "";
        for (String s:this.users)
                if (s.equals(user))
                        res = user;
        return res;
    }
    
    public ArrayList<String> listUsers (){
            return (this.users);
    }
    
}[/code]

My JSP client page:
[code]
<...>
<%_at_page import="helb.test.ejb.*,java.util.ArrayList"%>
<%_at_page import="javax.naming.InitialContext,javax.naming.NamingException" %>

<%
InitialContext ictx;
SessionManager ism;

        out.println("Start..." + "<br/>");

try {
                ictx = new InitialContext();
                ism = (SessionManager) ictx.lookup("ejb/SessionManagerJNDI");

             ism.addUser("Entry 1");
             ism.addUser("Entry 2");
     
     ArrayList<String> v = ism.listUsers();
     
     for (String user:v){
         out.println("- " + user + "<br/>");
     }
     
     out.println("Finished listing users" + "<br/>");
     out.flush();
     
        } catch (NamingException e) {

                StackTraceElement[] ste = e.getStackTrace();
                for (StackTraceElement S:ste)
                        out.println(S.toString() + "<br/>");
        }
%>
<...>[/code]
Am I missing something ?

Thanks :)
[Message sent by forum member 'goundy' (goundy)]

http://forums.java.net/jive/thread.jspa?messageID=316129