Introduction to Session Bean
A session bean is an irrecoverable JAVA packet created to encapsulate business logic. Any business logic which needs to be implemented over a network and should be maintained separately can be put into session bean. Session beans are invoked by local or remote beans over the network as per business requirements. Session bean abstracts the complex business logic from the end-user by invoking complex server processes by its get/set methods. This forms an important component of EJB (enterprise JAVA beans). This is mostly used over the network where we have distributed resources. As mentioned it is irrecoverable, this means that it is not persistent even if it is stateful session bean. The data contained in the session bean is not saved in the database.
Uses of Session Bean
The use of it is seen over the distributed network where resources are not present at a single location. This bean establishes a connection between different beans and client present in different locations by holding information which needs to be passed from source to destination. This abstracts all complex business logic from the user by encouraging clear and maintainable code files. Session beans do not store data in the database, confirming that its very lightweight and does not put a burden on the database. There are different types of session beans to improve the performance of the application by the smooth data flow. This bean is also used in implementing web service. Other functionalities along with its type can be understood well in the next section.
Types of Session Bean
There are three types and these are explained below:
1. Stateless
The State is not saved even when session bean interacts with multiple clients and other EJBs. Here we mean “data” when we refer to “state”. So this bean contains only business logic with no data in it. It does not maintain any conversation with the client as it does not save the state. Whenever a method for the stateless bean is invoked by a client, the state of a session bean is activated only for that time duration. As soon as the method functioning is stopped, the client state maintained is lost. The client can then pick up any stateless session from the pool of sessions and use that to implement other processes. The state is maintained only during the method invocation time period. This functionality makes this session bean as a stateless.
2. Stateful
The state is saved when a call is made between a source and destination bean or client. Here, the data is saved per session in case we need to maintain further communication so we have that enough information to start with that. There is only one connection between a specific client and its bean. This is linking is commonly called “conversational state”. It is not shared and is excluded from its dedicated client. The state is retained until the session between client and bean exists. In case the client wants to stop the session then session bean is dropped and interaction is stopped. This is commonly used when we need to maintain a link between various beans. For example, navigating through a customer amazon account. While navigating through the amazon account the state is maintained to track the customer orders, history of orders, initiating refunds, maintain product watch history, and many more functions. All these functions are dependent on the customer’s account and so the state becomes an important parameter to maintain it. Imagine a blunder if customer 1’s payment is recorded as payment of customer 2?
3. Singleton
It is a persistent bean invoked only one time per application. This is used in a case where multiple clients are sharing the single session bean concurrently. Singleton maintains their states between clients (as it interacts between multiple clients simultaneously) but do not maintain sessions in case of server break down or server crash. It works like stateless session beans but with an exception that it is only one handling multiple clients while stateless session beans are many more in number dealing with clients as to when required. This is used in a web service endpoints. It is used in the initialization of application and is used as a cleanup tool as it is used throughout the time till application runs.
Example to Implement Session Bean
Below is the example mentioned:
Example #1
We can use Netbeans or eclipse IDE to understand how session beans work. There have to be many files like one remote invocation file, another is a local file, and many more to connect it with the server. One will have to import EJB jar downloaded externally into your project in eclipse IDE. I created a project named “client1” in the EJB module of the eclipse IDE.
Added three files to it named:
- java: It contained the addition function with function definition and return type and value.
- java: It contained the interface and function name This function is declared here whose actual definition is provided in the previous file.
- java: This is the main program which will be executed as soon as the application is invoked. It contains all the relevant information to start up with the server and call the function stored in local bean “mybeanlocal” by calling a remote bean “mybeanremote”. “myremotebean” acts as a link between main and local bean.
File 1: mybeanlocal.java
Code:
package sample;
import javax.ejb.Stateless;
@Stateless(mappedName="test")
// Changing this annotation to "stateful" will ensure that this same example works for stateful case.
public class mybeanlocal implements mybeanremote{
@Override
public int addintegers(int a, int b) {
// TODO Auto-generated method stub
return a + b;
}
}
File 2: mybeanremote.java
Code:
package sample;
import javax.ejb.Remote;
public interface mybeanremote {
int addintegers(int a,int b);
}
File3: Main.java
Code:
package sample;
import javax.naming.Context;
import javax.naming.InitialContext;
public class Main {
public static void main(String[] args) throws Exception{
//TODO Auto-generated method stub
Context context=new InitialContext();
mybeanremote remote=(mybeanremote)context.lookup("test");
System.out.println("Output is : "+remote.addintegers(40,40));
}
/* (non-Java-doc)
* @see java.lang.Object#Object()
*/
public Main() {
super();
}
}
Output:
Conclusion
This plays an important role in creating enterprise applications that run on different sessions cerated while interacting with distributed objects in the network. The stateless and stateful application of session beans is used to apply a lightweight add-on to maintain application coherence ensuring smooth and continuous navigation and good customer experience. It is also used to hide and protect the complex business logic behind the scenes. It can be implemented via EJB modules or via JAVA depending upon the requirements and technical perspective.
Recommended Articles
This is a guide to Session Bean. Here we discuss an introduction to Session Bean, uses, three types, and example with code and output. You can also go through our other related articles to learn more –
600+ Online Courses | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6
View Course
Related Courses