Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Stateless vs Stateful beans

970232Oct 23 2012 — edited Nov 26 2012
Hi,

I am new to Java EE and EJB 3, so many concepts are not clear to me.

I've read somewhere that Stateless beans in Java do not keep their state between two calls from the client, unlike Stateful session beans which do keep state.

I created my first bean, I first used @Stateless annotation and then @stateful annotation, and it seems it is having the same behavior.

This is the session bean:
@Stateless
public class HelloWorldBean implements IHelloWorldLocal {
    private int number = 1;

    @Override
    public int add(Integer number) {
        this.number += number;
        return this.number;
    }

}
I wrote a servlet to invoke session bean add method this way:

            out.println("The result is " + helloWorld.add(2));
            out.println("The result is " + helloWorld.add(3));
            out.println("The result is " + helloWorld.add(4));
The results after these three invocations are 3, 6, 10. As far as I am concerned, results should be 3, 4 and 5 based on the concept that the Stateless beans don't keep state.

If I switch this bean to be a Stateful one, I get the same results.

What have I missed here?

Thanks in advance for your time and help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 24 2012
Added on Oct 23 2012
8 comments
2,478 views