2 Replies Latest reply on Jan 2, 2002 9:37 AM by Andreas Schouten

    How can client gets same statefull session bean back?

    boris Newbie

      hi! all,
      I wrote a statefull Session Bean and a client to test Session Bean.
      client is simplely to create a Session by ID(the type of ID is String) and set/get value to Session Bean. each time i run client to create a Session Bean I got a new Session Bean, even I have gave same ID to create sessoin. I check JBOSS and I'm sure JBOSS has stored information of Session Bean, but why I can't get same Session Bean back when next time I run Client again?


      the Home of session bean as below:

      import java.rmi.*;
      import javax.ejb.*;
      public interface JSSessionHome extends EJBHome
      {
      public JSSession create(String ID) throws RemoteException, CreateException;
      }

      thanks for help~~~ O_Q

      Boris

        • 1. Re: How can client gets same statefull session bean back?
          Paul Boddie Newbie

          > I wrote a statefull Session Bean and a client to
          > test Session Bean.
          > client is simplely to create a Session by ID(the
          > type of ID is String) and set/get value to Session
          > Bean. each time i run client to create a Session Bean
          > I got a new Session Bean, even I have gave same ID to
          > create sessoin. I check JBOSS and I'm sure JBOSS has
          > stored information of Session Bean, but why I can't
          > get same Session Bean back when next time I run
          > Client again?

          Unless I have misunderstood what you have done or what you are trying to do, I get the impression that you really want to use entity beans in this situation.

          While the client holds a reference to the session bean that you created (meaning "while the client is still running"), the session bean should behave statefully (meaning that it should remember things between operations performed on it), but by having the client terminate, the client obviously loses the reference to the bean, and this probably results in the session bean being discarded by the container (since nothing is using the bean any more).

          Now, if the client is restarted, it has to go and create a new bean and JBoss is not going to magically find the old bean just because the same ID is specified. You could start two clients at the same time and observe that they acquire references to different session beans.

          With entity beans, however, the "find me the bean with that ID" behaviour is supported. Therefore, I would use entity beans instead of session beans, if I were you.

          • 2. Re: How can client gets same statefull session bean back?
            Andreas Schouten Newbie

            If different clients need access to the same SFSB, You need passing the EJB handle between the clients.

            Or You need a factory object on the server which manage the ejb handles.
            Simply write a SLSB with a method
            getHandleById(String id)
            addHandleForId(String id, javax.ejb.Handle handle)
            and
            removeHandleForId(String id)

            In ejbCreate of the SFSB call the method of SLSF, which adds the handle to a *static* hashmap and on ejbRemove remove method removes the entry from the hashmap.

            Andreas