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 session bean confusion

 
Dwijen Bhattacharjee
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am very new to ejb and I have a confusion regarding stateless session bean.
I have created a instance variable inside the session bean ;and inside my business method I am appending the method parameter in the variable.ThenI am calling that method twice from client by passing the parameter "First" and" "Second" respectively and printing the value of the variable.
Now here is my confusion.As per my knowledge stateless session bean does not hold the state between successive method call.So i expected the output "First" and "Second" as during the 2nd call the variable should not contain any value during the first call.
But I am getting output as "First" and"FirstSecond". My confusion is Why in the second time it is holding the first paramvalue?
Below I pasted my code snippet :-
1) Session Bean Implementation Class

}
2) I am calling from client by :-
ejbRemote.returnString("First");
ejbRemote.returnString("Second");
3) ejb-jar.xml is as follwes
<ejb-jar id="ejb-jar_ID">
<display-name>EJBProject</display-name>
<enterprise-beans>
<session id="EJBProject">
<ejb-name>EJBProject</ejb-name>
<home>beans.EJBHomeInterface</home>
<remote>beans.EJBRemoteInterface</remote>
<ejb-class>beans.EJBBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>

Thanks in advance.

[edited to add code tags]
[ August 27, 2008: Message edited by: Jeanne Boyarsky ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dwijen,
The bean is functioning correctly. In fact it is functioning just like a normal Java object. It's not that the stateless session bean doesn't hold instance variables, it's that it shouldn't hold user state. You also can't rely on getting the same bean each time so the information may or may not be available. However if you are holding something general - like an instance to a DAO factory - all users of the bean would want it.
 
Dwijen Bhattacharjee
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne. Thanks a lot. but i didn't understand the last line "However if you are holding something general - like an instance to a DAO factory - all users of the bean would want it". I am new to EJB world .So I don't have detail knowledge. It will be great if you explain these more.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dwijen Bhattacharjee:
Thanks Jeanne. Thanks a lot. but i didn't understand the last line "However if you are holding something general - like an instance to a DAO factory - all users of the bean would want it". I am new to EJB world .So I don't have detail knowledge. It will be great if you explain these more.

Sure. I'll explain via two examples:

Bad practice. Don't do this:


Ok practice:


"Factory" and "DAO" are both patterns. A factory creates other objects - in this case the DAO objects. A DAO (data access object) accesses the datasource. For example, it might contain the actual JDBC code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic