Session Bean In Java

What is the session bean?

 
A session is a business programming unit that can be executed by a client remotely, locally or using a Web API or Web service method call.  Session beans are useful when you do not wish to give control to a client, but allow them to execute something on a server. 
 
When a client, or user, requests a session bean, a separate instance of the bean is created and provided to the caller.  A number of method calls from the bean creates a conversation between the client and the bean.  A session bean represents work performed by single a user and holds the state between method calls.  A session bean is non-persistent.  That means session beans are not stored in permanent storage.
 

How session bean differs from other bean types?

 
A session bean is different from other bean types in terms of the scope of their life, as it’s instance is comparatively a short-lived object.  Session beans have a lifetime equivalent of a session or client, calling the instance of a session bean.  The EJB container is responsible for creating an instance of a session bean and later when a client disconnects EJB, the container is empowered to destroy the session bean instance.  The Entity bean remains in memory for months, or years, depending on its settings.  In contrast, if a client is using session bean for 20 minutes, then it might be in memory for minutes to hours, but not weeks or months.
 

Type of session bean

  • Stateless session bean
  • Stateful session bean
  • Singleton session bean
      

Stateless bean

 
A stateless bean is used in a single method invocation, where we don’t need to retain the state from a method to a method call.  The stateless session bean could be reused and swapped, from one client to another, on each method call.  After each method invocation, the EJB container may destroy it, or clear all information related to the past method invocation.  An example of a stateless session bean is a registration form, where a session bean takes the information of one user and when another user comes, the session bean doesn’t retrain data, of the previous request.  The server, also has a lifecycle session bean that is maintained by the server, for the lifecycle of the application.
  

Lifecycle of a stateless bean

 
When nobody is calling a session bean, there are no instances.  Session bean instances are created only when a client is connected and demands it.  When a new request comes, for a new session bean, the EJB container decides whether to create a new instance, or not, depending upon the number of users.  If the user requests go beyond the limit, the server automatically creates new instances.  If the number of clients increases, the server automatically creates new instances.  In the stateless session bean, we may see 5 instances being shared by 5000 users.  These 5 instances are stored in memory called the instance pool.  When a request is completed, the object goes back to the pool and stays alive and available for the next request.
 

Stateless bean has four callback method

  • post construct
     
    Post construct callback is called as soon as an instance is created.  Once a post construct is called, you can easily invoke an instance of a session bean.
      
  • pre destroy
     
    pre destroy is called in a situation where the project is deployed.  Here you can free resources.
     
  • init
     
    You can perform any initialization task that you want to perform, before an instance is created.  For example, injecting dependency on which session bean depends.
     
  • remove
     
    When there is no client.  In that case, the server is going to wait for some time depending on the setting.  It can be hours, or weeks.  If there are no client requests during that time, the server removed the EJB bean.
So, the bean is destroyed in two ways.  First, when we deploy EJB and the second is decided by the server.  When a new client request comes in, a new instance is created and the lifecycle starts again.
 

Stateful bean

 
A stateful session bean is used to retain the state of an individual client.  During the method invocation, if the state of a method changes, then the client gets the updated state, in the next method invocation.  Stateful session beans are designed for conversation, which includes multiple method invocations, whose state must be retained and shared. We must use a stateful session bean in an application, where the user is updating any list.  For example, a user is selecting a number of channels on TV.
 

Lifecycle of stateful bean

 
In stateful bean, the number of instances is equal to the number of clients, which means if 5000 clients connect to the server, EJB will create 5000 instances, of the stateful bean.  This could be a big problem, but can be solved by passivation and activation.  In the stateful bean, all clients will be connected, but not all of them will call methods, at the same time.  Consider 5000 clients are connected,  but only 100 of them are actually calling methods. In this case, the server passivates the remaining 4900 clients and the state of their method are stored in virtual memory or secondary storage.  If any passivate clients request, the new instance is activated. 
 

Singleton session bean

 
Methods of session bean, 
  1. ejbCreate()
  2. ejbPassivate()
  3. ejbActivate()
  4. ejbRemove()

ejbCreate()

 
Used to perform any initialization task.  For example, initializing variables.  You must declare at least one ejbCreate() method, but you can define multiple ejbCreate() methods, with different arguments.

 

ejbPassivate()

 
This method is called immediately before the session bean is passivated.  In this method, we can release any resources held by the bean.  ejbPassivate() is warning the bean that its state is about to swap out.
 

ejbActivate()

 
This method is called immediately after the session bean is activated.  In this method, we can acquire resources that we have released during ejbPassivate().
 

ejbRemove()

 
ejbRemove() is clean-up method.  This method is called by the server, immediately before the bean is removed from memory and we can free all resources that we have allocated to the bean.

 

Summary 

 
In the article, we studied about session java beans