This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
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

Stateful and Stateless Session Bean reversed in JBoss?

 
frankie huang
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I deploy a stateful session bean, the state can't keep; while a deploy a stateless session bean,the state keep. why?
my session bean:

the ejb-jar.xml

the client code:

when I code "stateful" in the ejb-jar.xml, I found the result is always "1" even I refresh the browser times. when I code "stateless" in the ejb-jar.xml, I found the result is "1",and then "2" and then "3"...
I feel puzzling, it does not obey the book "Mastering EJB".
 
Sam Dalton
Author
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The stateless bean behavior is absolutely correct. A stateless beans member variables are always maintained (per bean instance) across calls by ALL clients. This means any client that calls your bean will increment the variable.
The fact that you are creating and then 'removing' your bean means that the stateful bean is lost with each call. If you take out the code that removes the bean, you will probably find that this works fine.
Sam
[ January 13, 2003: Message edited by: Sam Dalton ]
 
frankie huang
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though I remove the "remove" code,the result is not changed and the member's state can't keep. And I found the bean call "setSessionContext()" & "ejbCreate()" & "count()" every time I call it in the client. the STDOUT:
 
Sam Dalton
Author
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you call create() you are creating a NEW instance of your stateful bean (with default values for the fields) you need to, instead, store the reference to your Stateful bean and reuse that (the HttpSession is a good place to store such a reference)
Cheers
Sam
 
frankie huang
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sam Dalton:
when you call create() you are creating a NEW instance of your stateful bean (with default values for the fields) you need to, instead, store the reference to your Stateful bean and reuse that (the HttpSession is a good place to store such a reference)
Cheers
Sam

oh,I must keep it in Session manually? I see,Thank you!
 
Sam Dalton
Author
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct.
Let me know if it works OK for you.
Cheers
Sam
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic