Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

stateless vs stateful session bean

 
Wim Molenberghs
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wim Molenberghs
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wim,

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

Thanks,
Reza
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic