What is a Session bean
A session bean is the
enterprise bean that directly interact with the user and contains the business
logic of the enterprise application. A session bean represents a single client
accessing the enterprise application deployed on the server by invoking its
method. An application may contain multiple sessions depending upon the number of
users accessing to the application. A session bean makes an interactive session only
for a single client and shields that client from complexities just by
executing the business task on server side.
For example, whenever a client wants to perform any of these actions such as making a reservation or validating a credit card, a session bean should be used. The session bean decides what data is to be modified. Typically, the session bean uses an entity bean to access or modify data. They implement business logic, business rules, algorithms, and work flows. Session beans are relatively short-lived components. The EJB container may destroy a session bean if its client times out.
A session bean can neither be shared nor can persist (means its value can not be saved to the database) its value. A session bean can have only one client. As long as the client terminates, session bean associated with this client is also terminated and the data associated with this bean is also destroyed.
The above figure shows how Session Bean interacts with the clients as well as with the Entity Beans.
Session beans are divided into two parts.
The user interface calls methods of session beans if the user wants to use the functionality of the session bean. Session beans can call to other session beans and entity beans.
When to use session beans:
Generally session beans are used in the following circumstances:
Stateful session beans are useful in the following circumstances:
Stateless session beans are appropriate in the circumstances illustrated below:
Life Cycle of a Stateless Session Bean:
Since the Stateless session bean does not passivates across method calls therefore a stateless session bean includes only two stages. Whether it does not exist or ready for method invocation. A stateless session bean starts its life cycle when the client first obtains the reference of the session bean. For this, the container performs the dependency injection before invoking the annotated @PreConstruct method if any exists. After invoking the annotated @PreConstruct method the bean will be ready to invoke its method by the client.
The above figure demonstrates how the Stateless Session Beans are created and destroyed.
The container calls the annotated @PreDestroy method while ending the life cycle of the session bean. After this, the bean is ready for garbage collection.
Life Cycle of a Stateful Session Bean:
A Stateful session bean starts its life cycle when the client first gets the reference of a stateful session bean. Before invoking the method annotated @PostConstruct the container performs any dependency injection after this the bean is ready. The container may deactivate a bean while in ready state (Generally the container uses the least recently use algorithm to passivates a bean). In the passivate mechanism the bean moves from memory to secondary memory. The container invokes the annotated @PrePassivate method before passivating the bean. If a client invokes a business method on the passivated bean then the container invokes the annotated @PostActivate method to let come the bean in the ready state.
The above image shows the various states of the Stateful
While ending the life cycle of the bean, the client calls the annotated @Remove method after this the container calls the annotated @PreDestroy method which results in the bean to be ready for the garbage collection.
If you enjoyed this post then why not add us on Google+? Add us to your Circles
Liked it! Share this Tutorial