5 Replies Latest reply on May 4, 2002 5:57 AM by Filippo

    Stateless Session bean not thread safe?

    Filippo Newbie

      I'm facing my personal learning ramp, so I could be in error, but I've noticed that Stateless Session Beans seem not to be THREAD SAFE. I'm using win2k, jboss3.0rc2.
      I've implemented the following scenario:
      Declared a global int variable nDemo in EJB bean class.
      EjbCreate sets nDemo=0
      A simple method called by servlet increments variable nDemo
      Servlet displays variable's value.

      In standardjboss.xml I've set for my convenience 10 instead of 100 on Standard Stateless Session Bean.

      I noticed that on tenth calling EJB method increments an already incremented value, so servlet displays 2 instead of 1. Continuing in calling EJB method the value becomes 3,4,5,...
      That sound very strange to me because servlet calls ejbCrate and ejbRemove every time it calls ejb method.

      But there's a stranger thing. If ejbCreate doesn't set global variable to 0 but this result is achieved when declaring the variable itself (private int nDemo=0;), the game breaks every time on fourth calling to ejb method.

      The attach files mechanism doesn't not work!So I can't attach the foolowing files:
      1) demo.ear
      2) build containing classes and deployment files
      3) src containing sources.

      I'll retry later. In the meantime I can send you the .zip (17K) by mail.

      I hope someone would help.

        • 2. Re: Stateless Session bean not thread safe?
          Stephen Coy Novice

          Lets see. You have a stateless session bean, in which you are trying to maintain some state (nDemo). Can you see the problem with that?

          Try making it a stateful bean and see if that works properly.

          Even then, you should make yourself familiar with section 7.4.1 "Instance passivation and conversational state" of the EJB2.0 spec.

          • 3. Re: Stateless Session bean not thread safe?
            Filippo Newbie

            The stateful bean version works fine.
            I'm reading Mastering EJB 2ed. by Ed Roman in which I found that ejbActivate and ejbPassivate don't apply to stateless session beans. It doesn't mind whatever action is taken by container on stateless bean already in pool, what matters is that the container should clear those stateless beans. So it doesn't matter if I'm trying to hold a state in a stateless bean, the container should clear it every time for me because stateless beans are thread safe.
            Now, I don't want to hold a state in stateless bean 'cause it's quite insane, but for sake of testing I think it's seems not working very well.
            ciao

            • 4. Re: Stateless Session bean not thread safe?
              Stephen Coy Novice

              What makes you think that:

              a) The container should "clear" stateless beans?

              b) Stateless beans are "threadsafe"?

              • 5. Re: Stateless Session bean not thread safe?
                Filippo Newbie

                Sorry, I gave a bad problem's explanation.
                Beans are single-threaded so they must be thread safe. I was meaning the effect that I noticed seemed a result of a not thread-safe operation.

                Well, I think you're right because I've reread p.83 of Mastering EJB and I understood that I should expect my stateless bean to forget everything after each method call and NOT I should expect my container make the bean forget everything. That's a substantial difference!
                I've also learned that I shouldn't rely on ejbCreate,ejbRemove method because this is a container's matter.

                Thanks for time you spent.