1 Reply Latest reply on May 15, 2006 4:01 AM by schoenen

    Stateless is not Stateless ??

    feristhia

      Hi All,

      Just curious with JBoss Stateless Session Bean. I currently have jboss-4.0.3SP1 running as my application server. Then I try to deploy a simple stateless session bean.

      I have 2 interfaces (home and remote) and the bean class as follows

      //PlayerHome.java
      
      package jndi.bean;
      
      import java.rmi.RemoteException;
      
      import javax.ejb.CreateException;
      import javax.ejb.EJBHome;
      
      public interface PlayerHome extends EJBHome {
       public Player create() throws RemoteException, CreateException;
      }
      
      


      //Player.java
      package jndi.bean;
      
      import java.rmi.RemoteException;
      
      import javax.ejb.EJBObject;
      
      public interface Player extends EJBObject {
       public String getPlayerID() throws RemoteException;
      
       public void setPlayerID(String playerid) throws RemoteException;
      }
      
      


      //PlayerBean.java
      package jndi.bean;
      
      import java.rmi.RemoteException;
      import java.util.*;
      
      import javax.ejb.EJBException;
      import javax.ejb.SessionBean;
      import javax.ejb.SessionContext;
      
      public class PlayerBean implements SessionBean {
       private String playerID;
       private String position;
       private String playerName;
       private int salary;
      
       Collection objCollection;
      
       public String getPlayerID()
       {
       return playerID;
       }
      
       public void setPlayerID(String playeridvalue)
       {
       playerID = playeridvalue;
       }
      
       public String getPlayerName() {
       return playerName;
       }
      
       public void setPlayerName(String playerName) {
       this.playerName = playerName;
       }
      
       public String getPosition() {
       return position;
       }
      
       public void setPosition(String position) {
       this.position = position;
       }
      
       public int getSalary() {
       return salary;
       }
      
       public void setSalary(int salary) {
       this.salary = salary;
       }
      
       public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {
       // TODO Auto-generated method stub
      
       }
      
       public void ejbCreate() {
      
       }
      
       public void ejbRemove(){
       // TODO Auto-generated method stub
      
       }
      
       public void ejbActivate() throws EJBException, RemoteException {
       // TODO Auto-generated method stub
      
       }
      
       public void ejbPassivate() throws EJBException, RemoteException {
       // TODO Auto-generated method stub
      
       }
      }
      
      


      and the deployment descriptor

      <?xml version="1.0" encoding="UTF-8"?>
      <ejb-jar id="ejb-jar_ID" version="2.1"
       xmlns="http://java.sun.com/xml/ns/j2ee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
       <description>jBoss test application</description>
       <display-name>Player Test</display-name>
       <enterprise-beans>
       <session>
       <ejb-name>PlayerBean</ejb-name>
       <home>jndi.bean.PlayerHome</home>
       <remote>jndi.bean.Player</remote>
       <ejb-class>jndi.bean.PlayerBean</ejb-class>
       <session-type>Stateless</session-type>
       <transaction-type>Bean</transaction-type>
       </session>
       </enterprise-beans>
      </ejb-jar>
      


      and all the code deployed successfully.... I try to access that from the client

      //PlayerTest.java
      import java.util.*;
      
      import javax.naming.InitialContext;
      import javax.rmi.PortableRemoteObject;
      
      import jndi.bean.*;
      
      public class PlayerTest {
      
       public static void main(String[] args) {
      
       try
       {
       Properties prop = new Properties();
       prop.setProperty("java.naming.factory.initial",
       "org.jnp.interfaces.NamingContextFactory");
       prop.setProperty("java.naming.provider.url",
       "192.168.100.34:1099");
      
       InitialContext ctx = new InitialContext(prop);
      
       Object ref = ctx.lookup("PlayerBean");
      
       PlayerHome myHome = (PlayerHome) PortableRemoteObject.narrow(ref,PlayerHome.class);
      
       Player myplayer = (Player) myHome.create();
      
       myplayer.setPlayerID("J-001");
      
       System.out.println(myplayer.getPlayerID());
       }
       catch(Exception e)
       {
       e.printStackTrace();
       }
      
       }
      }
      


      it will print :


      J-001


      as the result.

      Then.... I try to call it without set any value ... so I remove the following code from PlayerTest.java

       myplayer.setPlayerID("J-001");
      


      What I expected is a null value. But it comes out "J-001" as the result. Then I try to access from another client.... still got "J-001".

      How is it happen ? Is there something wrong about my code ? Why JBoss cache the Stateless while it should be removed after a client instance is remove ?

      Regards,

      Feris

        • 1. Re: Stateless is not Stateless ??
          schoenen

          Hi,

          your bean is only "called" stateless, but in fact it is not! If you call it the first time and set a value to use it later, you expect a state. It is bad programming, to hand out a value (getPlayerId()), which was set or not in an other step.

          "Stateless" means to ask for a value, which is independent from any settings; e.g. ask for the time, or the weather conditions, or a value from a data base, or let him calculate something from a parameter you give him IN ONE STEP.

          You shold expect that JBoss probably will reuse the instantiated bean and perhaps destroy it, if it is not further used for a couple of time.

          Happy testing
          Holger