Java Reference
In-Depth Information
// Create remote interface
HelloEJB bean = (HelloEJB)home.create();
// Invoke EJB services through remote interface reference
String[] messages= bean.getAllWelcomeMsgs();
toBrowser("<BR>Available messages:", out);
for(int i=0; i<bean.getNumberOfWelcomeMsgs(); i++) {
toBrowser(messages[i], out);
}
//Test out-of-range index
toBrowser("<BR>", out);
toBrowser("Trying message No. -1", out);
toBrowser("Message No. -1: " + bean.getWelcomeMsg(-1), out);
toBrowser("<BR>", out);
toBrowser("Trying message No. 4", out);
toBrowser("Message No. 4: " + bean.getWelcomeMsg(4), out);
}
%>
Stateful Session Bean
A stateful session bean typically implements a conversational business process. A shopping cart of an
online shopping application is a classical example of a stateful session bean. While a shopper searches
the catalog and keeps dropping items into his or her shopping cart, the item list must be maintained.
Obviously, different shoppers' shopping carts cannot be mixed. Only after a shopper finally checks out
are the purchased items transferred into a persistent data store (such as a database).
A shopping cart application differs from a catalog-search application, for example, because each time
the user searches the catalog, the search criteria are different. Such a service is usually implemented
by a stateless session bean. This means that, unlike stateless session beans, a stateful session bean
cannot serve multiple clients. An instance of a stateful session bean is associated with only one client.
The instance retains the state on behalf of the client across multiple method invocations.
There is a one-to-one correspondence between user sessions (maintained as HttpSession objects)
and the instances of a stateful session bean. The EJB container always delegates the method
invocation from a given client to the same stateful session bean instance. The instance variables of the
stateful session bean provide a convenient mechanism for the application developer to retain a client-
specific state on the server. Note that such a state is not persistent on any data store. If the session is
timed out, or if the server is crashed, the states are lost. If the states need to be persistent against
server crash, entity beans must be used.
A client initiates the life cycle of a stateful session EJB in the same way as stateless session beans: by
invoking the create method in its home interface. The EJB container instantiates the bean and then
invokes the setSessionContext and ejbCreate methods in the session bean. The bean is now
ready to have its business methods invoked.
Unlike stateless session beans, stateful session instances cannot be pooled because of the one-to-one
correspondence between bean instances and session objects. At the end of the client session (for
example, the online shopper checks out), the client invokes the remove method, and the EJB container
calls the bean's ejbRemove method. The bean's instance is then ready for garbage collection.
Search WWH ::




Custom Search