EJBs in Java
In this article, I will explain what is an EJB, the key differences between JavaBeans and EJBs, and why EJBs are essential for building enterprise applications.
Overview
- Application client container manages the execution of application client components. Application clients and their container run on the client.
- Web Container manages Servlets and JSPs , which are used to build dynamic web pages. It handles HTTP requests and responses, manages user sessions, and offers basic security and transaction support.
- EJB Container manages EJBs, which are used to build scalable, transactional, and secure enterprise applications. It provides advanced services like automatic transaction handling, concurrency management, and security, allowing developers to focus on business logic instead of infrastructure.
What are EJBs?
EJBs are a component of Java EE (Jakarta EE) that provide a standardized way to build robust, scalable, and transactional enterprise applications. They simplify the development process by offloading the complexities of threading, transactions, and security to the EJB container.
Why Use EJBs Over Regular JavaBeans?
Let me explain this using an example.
Imagine an eCommerce shopping platform where customers can place orders, and you need to handle operations like updating inventory, processing payments, and saving order details.
Transaction Management
When a customer places an order, multiple operations need to succeed or fail together to maintain data consistency. For example, updating the inventory, processing the payment, and saving the order details should be part of a single transaction.
Limitations with POJOs:
- With normal JavaBeans (POJOs), you will have to manually manage transactions using JDBC or other database APIs. You need to start, commit, and roll back transactions explicitly in your code.
- If something goes wrong, you have to ensure that previous operations are rolled back to maintain consistency. This requires writing additional code and handling exceptions properly.
Concurrency Handling
If two customers try to order the last item in stock at the same time, you need to ensure that only one order is processed successfully and the stock level is updated correctly.
Get Dharshitha Senevirathne’s stories in your inbox
Join Medium for free to get updates from this writer.
Limitations with POJOs:
- With normal JavaBeans (POJOs), you need to manually handle concurrent access to shared resources. This might involve using synchronized blocks or locks to prevent race conditions.
- Ensuring thread safety can be complex and error-prone, especially as the number of users grows.
Types of EJBs
There are three main types of EJBs, each serving a distinct purpose.
Session Beans
A Session Bean is a type of EJB designed to encapsulate business logic and manage interactions between a client and the application.
Stateless Session Beans: Ideal for tasks that do not require storing client-specific data between calls, such as performing calculations, executing business rules, or handling simple operations.
@Stateless
public class TaxCalculatorBean {
public double calculateTax(double amount) {
return amount * 0.15;
}
}Stateful Session Beans: Suitable for scenarios where the bean needs to maintain information between method calls, such as managing user sessions or handling shopping carts.
@Stateful
public class ShoppingCartBean {
private List<Item> items = new ArrayList<>();
public void addItem(Item item) {
items.add(item);
}
public List<Item> getItems() {
return items;
}
}Message-Driven Beans
Message driven beans are used to handle asynchronous message processing, often used in messaging systems like JMS (Java Message Service).
Suitable for scenarios where the application needs to process messages or events without blocking the main application flow, such as handling incoming orders or processing notifications.
@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "java:/jms/queue/OrderQueue")
})
public class OrderProcessorMDB implements MessageListener {
@Override
public void onMessage(Message message) {
// Process the incoming order message
}
}Conclusion
By using EJBs, you can ensure your applications handle large numbers of users, complex transactions, and high concurrency without needing to manually write code to manage these challenges. This makes EJBs a critical part of enterprise Java development, suitable for building modern, scalable, and maintainable systems.










