Updated July 3, 2023
Introduction to Session Bean
A session bean is a Java component designed to encapsulate and manage business logic within an application. It serves as a unit of functionality that can be accessed remotely over a network. Local or remote beans invoke session 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). As mentioned, it is irrecoverable; this means that it is not persistent even if it is a stateful session bean.
Uses of Session Bean
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 it’s very lightweight and does not burden the database. There are different types of session beans to improve the performance of the application by the smooth data flow. Other functionalities, along with their 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. The client can then pick up any stateless session from the pool of sessions and use that to implement other processes. This functionality makes this session bean stateless.
2. Stateful
There is only one connection between a specific client and its bean. For example, navigating through a customer’s Amazon account. All these functions depend on the customer’s account, 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
A singleton session bean is a type of session bean that is instantiated only once per application and shared among multiple clients concurrently. Singleton maintains their states between clients (as it interacts between multiple clients simultaneously) but does not maintain sessions in case of server breakdown or server crash.
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 must 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.
- java: It contains all the relevant information to start up with the server and calls the function stored in the local bean “mybeanlocal” by calling a remote bean “mybeanremote.” “myremotebean” acts as a link between the 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 created while interacting with distributed objects in the network. The application of session beans, whether stateless or stateful, serves to enhance application coherence, ensuring seamless navigation and a positive user experience. These session beans provide a lightweight solution to maintain the application’s state, effectively hiding and safeguarding the intricate business logic.
Recommended Articles
This is a guide to Session Bean. Here we discuss an introduction to Session Bean, its uses, three types, and examples with code and output. You can also go through our other related articles to learn more –