wood burning stoves*
The moose likes EJB and other Java EE Technologies and the fly likes Can we maintain state with a stateless session bean 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 "Can we maintain state with a stateless session bean" Watch "Can we maintain state with a stateless session bean" New topic
Author

Can we maintain state with a stateless session bean

Aj Mathia
Ranch Hand

Joined: Apr 11, 2003
Posts: 478
Is it possible to achieve the funtionality of a stateful session bean while using a stateless bean.
this question was asked in a interview that i faced.


You think you know me .... You will never know me ... You know only what I let you know ... You are just a puppet ... --CMG
Pearlo Muthukumaran
Ranch Hand

Joined: Sep 17, 2002
Posts: 79
What you can possibly do is to have the state information bundled into an object and store this object with an some id in a static hashtable external to EJB contained in a singleton class but this information must also be passed to the client so that the client can retrieve the state information..........
If persistence is required, then you may have to serialize the state information.........
Other option is a good old one.......dump everything to database.... so that you get the same effect.....
Literally, you end up doing the job of the container......
Hope this helps
Rgds
Muthu
Geeta Ramasami
Ranch Hand

Joined: Mar 05, 2003
Posts: 72
Hi,
The main difference that lies between the stateful,and stateless bean is the use of the bean instance.The container ensures that the ejbObject uses the same bean insatnce(ie the values of the instance variables) during the life cycle of the client in the stateful session bean.
But in stateless the same client calling a different method on a bean may end up using a different instance.So to achieve the functionality of stateful in stateless copy the value of the instance variables to the bean each time a method is invoked on the bean by the client.
If something is incorrect in the explanation provided, pl do let me know.
Cheers
Geeta
Aj Mathia
Ranch Hand

Joined: Apr 11, 2003
Posts: 478
Thank you for the tips i guess it make perfect sense
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
Hi Ajay,
Can you just take a quick look at our naming policy and edit your display name accordingly?
Cheers
Simon
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16065
    
  21

Hint: stateless EJB.
If a given EJB attempts to maintain state information inside itself, it's a stateful bean, whether so declared or not. And if it's not, the state may not get properly maintained, especially under heavy system load.
A stateless EJB is an object of which there may be multiple identical instances. That is, if you dive into the bean pool and come up with one at random, you will get extactly the same results no matter how many times you grab a bean instance. The only way a stateless bean could achieve that quality is to have whatever state was being pushed in propagated to every other bean in the pool. Which, needless to say, could be really horrible in terms of overhead.
Stateless Session EJBs are great as a repository for pure business logic. They have about the lighest overhead of any EJB. However if you need something that retains information between method calls, you should be using a stateful EJB - either a Stateful Session EJB, or, if persistency is important, an Entity EJB.
[ April 22, 2003: Message edited by: Tim Holloway ]

Customer surveys are for companies who didn't pay proper attention to begin with.
Dave Cronin
Ranch Hand

Joined: Apr 25, 2003
Posts: 114
A stateful session may have conversational state, that is, state belonging to a conversation with a client. Stateful session beans are designed to hold state for a client - each bean is unique, and can be passivated to conserve overhead.
On the other hand, a stateless session bean may not hold conversational state for a client. Although it's called a "stateless" session bean, a stateless session bean could hold state in a general sense, but not for a single client. It could hold a reference to an external resource like a database connection, for example.
Probably you can't directly achieve the functionality of a stateful session bean with a stateless session bean. But you could handle the client conversational state elsewhere, say in an HttpSession, or in the client application, or even in a database.
Have a look at the EJB spec, section 7.2 for stateless session beans.


Dave Cronin
SCJP, SCWCD, SCBCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we maintain state with a stateless session bean