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

    Stateless is not Stateless ??

    Feris Thia Newbie

      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

      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;

      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;

      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"
       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>

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

      import java.util.*;
      import javax.naming.InitialContext;
      import javax.rmi.PortableRemoteObject;
      import jndi.bean.*;
      public class PlayerTest {
       public static void main(String[] args) {
       Properties prop = new Properties();
       InitialContext ctx = new InitialContext(prop);
       Object ref = ctx.lookup("PlayerBean");
       PlayerHome myHome = (PlayerHome) PortableRemoteObject.narrow(ref,PlayerHome.class);
       Player myplayer = (Player) myHome.create();
       catch(Exception e)

      it will print :


      as the result.

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


      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 ?



        • 1. Re: Stateless is not Stateless ??
          Holger Schoenen Newbie


          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