Session Bean
A session bean encapsulates
business logic that can be invoked programmatically by a client over local,
remote, or web service client views. The session bean performs work for its
client, by executing business tasks inside the server.
A session bean is not persistent.
Session beans are of three types:
·
Stateful
·
Stateless
·
Singleton.
A stateful session bean is a type of enterprise bean which preserve the
conversational state with client. A stateful session bean as per its name keeps
associated client state in its instance variables. EJB Container creates a
separate stateful session bean to process client's each request. As soon as
request scope is over, statelful session bean is destroyed.
The state of an object is the values of
its instance variables. In a stateful session bean, the instance
variables represent the state of a unique client/bean session.
A session bean is not shared; it can
have only one client. When the client terminates, its session bean terminates
and is no longer associated with the client.
The state is retained for the duration
of the client/bean session. If the client removes the bean, the session ends
and the state disappears.
Stateful session beans are appropriate in any of the following
conditions.
·
The bean’s state represents the interaction between the bean and a
specific client.
·
The bean needs to hold information about the client across method
invocations.
·
The bean mediates between the client and the other components of the
application, presenting a simplified view to the client.
A stateless session bean is a type of
enterprise bean which is normally used to do independent operations. A
stateless session bean does not have any associated client state, but it may
preserve its instance state. EJB Container normally creates a pool of few
stateless bean's objects and use these objects to process client's request.
A stateless session bean does
not maintain a conversational state with the client. When a client invokes the
methods of a stateless bean, the bean’s instance variables may contain a state
specific to that client but only for the duration of the invocation. When the
method is finished, the client-specific state should not be retained.
Because they can support multiple
clients, stateless session beans can offer better scalability for applications
that require large numbers of clients. Typically, an application requires fewer
stateless session beans than stateful session beans to support the same number
of clients.
A stateless session bean can implement
a web service, but a stateful session bean cannot.
Stateless session bean are appropriate in following circumstances
·
The bean’s state has no data for a specific client.
·
In a single method invocation, the bean performs a generic task for all
clients.
·
The bean implements a web service.
A singleton
session bean is instantiated once per application and exists for the
lifecycle of the application. Singleton session beans are designed for
circumstances in which a single enterprise bean instance is shared across and
concurrently accessed by clients.
Singleton session beans offer similar
functionality to stateless session beans but differ from them in that there is
only one singleton session bean per application.
Singleton session beans maintain their
state between client invocations but are not required to maintain their state
across server crashes or shutdowns.
Applications that use a singleton
session bean may specify that the singleton should be instantiated upon
application startup, which allows the singleton to perform initialization tasks
for the application. The singleton may perform cleanup tasks on application
shutdown as well, because the singleton will operate throughout the lifecycle
of the application.
·
State needs to be shared across the application.
·
A single enterprise bean needs to be accessed by multiple threads
concurrently.
·
The application needs an enterprise bean to perform tasks upon
application startup and shutdown.
·
The bean implements a web service.