GeeCON Prague 2014*
The moose likes EJB and other Java EE Technologies and the fly likes Stateful or 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 "Stateful or stateless session bean?" Watch "Stateful or stateless session bean?" New topic
Author

Stateful or stateless session bean?

Jim Baker
Ranch Hand

Joined: Mar 10, 2002
Posts: 177
Hi,
I'm designing a system something like:
WebBrowser->Servlet->SessionEJB->DAO->DB
There are conversational states to maintain
between browser and servlet. So I let servlet
control the session.
Should I use stateful or stateless sessionEJB?
Since there is only one instance of servlet
in webserver for all the browser clients, there
is no way I can maintain correct conversational state
information for each client between servlet and sessionEJB in
the EJB container.
So I should only use stateless sessionEJB and have to
pass all the state information back and forth
between and EJB and servlet.
Please correct me if I'm wrong.
JB
Rick Salsa
Ranch Hand

Joined: Jul 17, 2001
Posts: 173
Originally posted by Jim Baker:

There are conversational states to maintain
between browser and servlet. So I let servlet
control the session.
Should I use stateful or stateless sessionEJB?
Since there is only one instance of servlet
in webserver for all the browser clients, there
is no way I can maintain correct conversational state
information for each client between servlet and sessionEJB in
the EJB container.
So I should only use stateless sessionEJB and have to
pass all the state information back and forth
between and EJB and servlet.

I'm not too sure I understand what you're saying. What sort of state are you wanting to maintain? Usually if you need to maintain some state between transactions, page views, etc, would would use a Stateful SessionBean.
Maybe you could provide a bit more detail idea of the problem?
/rick
[ April 03, 2002: Message edited by: rick salsa ]
Jim Baker
Ranch Hand

Joined: Mar 10, 2002
Posts: 177
Rick,
It is like online shopping. You browse the
products, select some of them, and put
them in shoppingcart. Once you are done, you
can check out. It more like a view navigation
for each client session.
Thanks.
JB
Kirt Henrie
Greenhorn

Joined: Mar 29, 2002
Posts: 17
Either the Servlets HttpSession object (retrieved from the request object request.getSession(true)), or a stateless session bean can be used to store an applications state.
I use the HTTP session myself because the app server I use supports statefull failover on HTTP sessions but not statefull session beans. You may want to research how your application server deals with failover if you want to use it in a clustered environment.
I have found that statefull session beans do not perform well due to the fact that each client has a unique instance. As you probably know, with the services EJB's provide comes a significant amount of overhead on the server.
On the other hand, a single servlet can service many clients (provided it is thread safe), and in conjunction with statless session beans provides good performance.
My typical architecture:
Client -> Front Controller (Servlet) -> Stateless Session Bean -> DAO/EntityBean.
All data retrieved from the persistence tier is stored in the HttpSession, or if it specific to a single page in the HttpRequest. The request/response is then forwarded from the servlet to the view JSP where the data is retrieved and displayed. In this architecture state between the servlet and session bean is not necessary, state stays at the web tier.
I believe that maintaining client state in both the Servlet and SessionBean would be excessive. In your case you could just store your shopping cart entries in a custom object, HashMap, or whatever and put it in the HttpSession.
KJ
Jim Baker
Ranch Hand

Joined: Mar 10, 2002
Posts: 177
KJ:
Thanks for the response.
Are U using sessionID created in web tier and
pass it to ejb tier to maintain state info?
If U use stateless session EJB, U must develop
some kind of state pool for the stateless EJB
to get state info in ejb tier.
Or U pass the state info to ejb tier from
web tier? It will increase network load.
How U make the front controller thread safe
if the client load is suddenly very high?
Multi-front controllers?
JB
Kirt Henrie
Greenhorn

Joined: Mar 29, 2002
Posts: 17
JB,
My EJB tier is not statefull at all. The servlet container maintains a session object for all current sessions on the server, therefore the front controller is not statefull either which is why it is thread safe. A single servlet instance by nature is designed to handle hundreds of clients. It should be thread safe unless it has client specific instance variables. All instance variables will be shared by all clients. My servlet basically gets the callers session by calling request.getSession() then stores any data inside the session by calling session.setAttribute(). My session beans do not have state either (no instance variables), so all parameters are passed into the method and the return type is of course the result of my business logic.
In the event that my server gets swarmed with requests, it will create a new servlet instance which will have access to the session objects.
And because my server (Orion) has statefull failover, if one server goes down the session objects become available for another server to pick up.
KJ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stateful or stateless session bean?