File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes stateless vs stateful session bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "stateless vs stateful session bean" Watch "stateless vs stateful session bean" New topic
Author

stateless vs stateful session bean

Wim Molenberghs
Greenhorn

Joined: Jan 31, 2008
Posts: 16
Hi ranchers,

I have some doubts about stateless en stateful session beans and I hope someone can point me to the right direction.

I've written the following code:

the bean:


and the client:


I would expect that, since it is a stateless bean, the output from the client is always 1 but if I run the client multiple times, the output is 1,2,3,4,...
So why keeps my bean his state and looks my stateless bean like a stateful??

Now, if I replace in my ejb the @stateless with @stateful


If I run my client, I expect an output of 1,2,3,4,... but instead, I get always an output of 1
So why doesn't keeps my bean his state and looks my stateful bean like a stateless??

Thanks!
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
Wim,

The stateless case is "working" accidentally because of the fact that the container only has one bean instance in the pool that is being re-used across look-ups/calls. Try running the clients in parallel and you will see a different result because the container will create more bean instances in the pool.

The stateful bean behavior is correct also. State should be maintained between invocations from the same client, not different clients (defined by the bean instance look-up).

Out of curiosity, what EJB 3 reference are you using? Most should have explained why is the case?

Regards,
Reza


Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
Wim Molenberghs
Greenhorn

Joined: Jan 31, 2008
Posts: 16
Thanks Reza

Reza Rahman wrote:
The stateless case is "working" accidentally because of the fact that the container only has one bean instance in the pool that is being re-used across look-ups/calls. Try running the clients in parallel and you will see a different result because the container will create more bean instances in the pool.


So a good practice is to never use instance variables which could have a state?

Reza Rahman wrote:
The stateful bean behavior is correct also. State should be maintained between invocations from the same client, not different clients (defined by the bean instance look-up).


Got it! I thought that if you run the client 2 times, it was like the same client.
But if a client is defined by the bean instance look-ups, I understand why my code always returns 1.

Reza Rahman wrote:
Out of curiosity, what EJB 3 reference are you using? Most should have explained why is the case?


I followed a course which was apparantly not good enough
I think I'll order a good book from Amazon tomorrow.
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
Wim,

Yes, stateless session bean instance variables should never be used to store application state.

Thanks,
Reza
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: stateless vs stateful session bean
 
Similar Threads
Stateless session bean!!!
Confused with Stateless Session Bean
Problem of dependency injection in jboss
Stateful session beans and JNDI-Lookup in Netbeans 6.8/EJB 3.1 does not work
Stateless vs Stateful Session Beans