4 Replies Latest reply on Jan 21, 2005 7:20 AM by efcorpa

    Is this a bug? stateful session bean is acting like stateles

    siupetpet

      Hello, please read my post and give me some insight on this "strange" behavior. I am in the learning stage of understanding the difference between stateful and stateless session beans. Here is what I did:

      I've created a stateLESS, container managed session bean; in the bean class there is a private int testInt variable, and there's a public void increment() method that increases testInt by 1 and then prints its value using System.out.println(testInt). This method is properly declared in the remote interface. In the ejbCreate, ejbRemove, setSessionContext, etc methods, there's System.out.println("ejbCreate"), System.out.println("ejbRemove"), System.out.println("setSessionContext") statements for tracing/debugging purpose.
      My ejb-jar.xml file looks like

      <ejb-jar>
      <enterprise-beans>

      <ejb-name>TestBean</ejb-name>
      TestHome
      Test
      <ejb-class>TestBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>

      </enterprise-beans>
      <assembly-descriptor>
      <container-transaction>
      Transaction attributes for 'TestBean' methods

      <ejb-name>TestBean</ejb-name>
      <method-name>*</method-name>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      </assembly-descriptor>
      </ejb-jar>

      Then the client does the usual lookup("TestBean") without a hitch, and calls the increment() method several times. The interesting thing is, on the JBoss console, I see
      [stdout] setSessionContext
      [stdout] ejbCreate
      [stdout] 1
      [stdout] 2
      [stdout] 3
      [stdout] 4
      ...


      ON THE OTHER HAND, if I change the <session-type> attribute from stateLESS to stateFUL, and keep the rest the same, I see the following on the JBoss console when I make several calls to the increment() method:
      [stdout] setSessionContext
      [stdout] ejbCreate
      [stdout] 1
      [stdout] setSessionContext
      [stdout] ejbCreate
      [stdout] 1
      [stdout] setSessionContext
      [stdout] ejbCreate
      [stdout] 1
      ...

      Don't stateful and stateless session beans have their meanings inverted???

      Please please help me understand this. Thank you so much :)

      I don't know if I should post all the codes, since it will take up extra space on the forum.

        • 1. Re: Is this a bug? stateful session bean is acting like stat
          gps

          No, not a bug. Correct behavior.
          It's most likely you misunderstand what SFSBs and SLSBs are.
          Thanks.

          • 2. Re: Is this a bug? stateful session bean is acting like stat
            jamesstrachan

            This requires a longer explanation.

            For stateless session beans, JBoss uses a pool of beans and supplies the client with any instance. In a idle system (normal developer mode), JBoss will create a pool with one instance and give you that instance on each call.

            Your client does a create() and then an increment() four times. So JBoss creates an instance on the first call, and then reuses it three times.

            That is why the variable is incremented. But, on a busy system, there would be more than one instance and you would get a different instance on each call. You would no longer see the apparent "stateful" behaviour.

            On the stateful session bean, you are again doing a create() and then an increment(). But, in this case, each create() creates a new instance of the Stateful Bean. If you really want to use a Stateful Session Bean, you should create it only once from the client and then increment it four times.

            James

            • 3. Re: Is this a bug? stateful session bean is acting like stat
              siupetpet

              Thank you so much James, I think you are totally right.
              I did some further testing by doing System.out.println(this); in the bean class, so that I can see the bean's memory location on the jboss console.
              For a slsb, the memory location has been always the same across multiple create() calls even from two different pc's and calls made several hours apart. This confirms that jboss only keeps one instance in the pool in my near-idle environment.

              On the other hand, if I change the bean to a sfsb in ejb-jar.xml, System.out.println(this) prints a different memory location with each create() call! This behavior again matches your description :)

              Thank you again James, now I think I got it!

              • 4. Re: Is this a bug? stateful session bean is acting like stat
                efcorpa

                Hi James,

                I've never worked with Stateful EJBs and I've got a doubt:

                you said "if you really want to use a Stateful Session Bean, you should create it only once from the client and then ..."

                but...

                what happens if client is a servlet?: you cannot store EJB as a Servlet's attribute since Servlet is multithreaded; so you need to call objEJBSF = objEJBSFHome.create() every time you need to process a request. However, if a new instance is created every time then you lose all the information you had stored in EJB object; which could be the solution?

                thanks in advance