Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we maintain state with a stateless session bean

 
Aj Mathia
Ranch Hand
Posts: 478
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pearlo Muthukumaran
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 478
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the tips i guess it make perfect sense
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Dave Cronin
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic