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 and other Java EE Technologies and the fly likes Stateless session bean confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Stateless session bean confusion" Watch "Stateless session bean confusion" New topic
Author

Stateless session bean confusion

Dwijen Bhattacharjee
Greenhorn

Joined: Mar 31, 2008
Posts: 26
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

Joined: May 26, 2003
Posts: 31062
    
232

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Dwijen Bhattacharjee
Greenhorn

Joined: Mar 31, 2008
Posts: 26
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

Joined: May 26, 2003
Posts: 31062
    
232

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stateless session bean confusion