Stateful Session Bean


Start course

In this course, we will learn the concepts of microservice and spring framework and focus on Enterprise Java Bean.

Learning Objectives

  • Enterprise Java Bean

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud


  • Basic Java knowledge

Hello dear friends. In this video, we'll examine Session Beans and Stateful beans. So, let's begin. Stateful session bean is a business object that represents business logic like stateless session bean. Stateless beans provide business methods to their clients, but don't maintain a conversational state with them. Stateful session beans on the other hand preserve conversational state. They are useful for tasks that have to be done in several steps, each of which relies on the state maintained in a previous step. Stateful session beans usually hold information about a specific client session and hold that information throughout the whole session. A stateful EJB instance is coupled with only one client. One client can of course have many EJB instances in a session.

Let's take the example of a shopping cart in an e-commerce website. A customer logs on, chooses the first product, adds it to a shopping cart, chooses a second product and adds it to his cart. At the end, the customer checks out the products, pays for them and logs out. The shopping cart keeps the state of how many products the customer has chosen throughout the interaction. A session bean is not shared. It can have only one client in the same way that an interactive session can have only one user. When the client terminates, its session bean appears to terminate and is no longer associated with the client. The state is retained for the duration of the client/bean session. If the client removes the bean, the session ends and the state disappears. This transient nature of the state is not a problem. However, because when the conversation between the client and the bean ends, there is no need to retain the state.

There are five important annotations used in stateful session bean. Stateful, PostConstruct, PreDestroy, PrePassivate, PostActivate. Now, let's look at the life cycle of stateful bean. The client initiates the life cycle by obtaining a reference to a stateful session bean. The container performs any dependency injection and then invokes the method annotated with PostConstruct, if any. The bean is now ready to have its business methods invoked by the client. While in the ready stage, the EJB container may decide to deactivate or passivate the bean by moving it from memory to secondary storage. The EJB container invokes the method annotated PrePassivate, if any, immediately before passivating it. If a client invokes a business method on the bean while it is in the passive stage, the EJB container activates the bean, calls the method annotated PostActivate, if any, and then moves it to the ready stage. At the end of the life cycle, the client invokes a method annotated remove and the EJB container calls the method annotated PreDestroy if any.

The beans instance is then ready for garbage collection. Your code controls the invocation of only one lifecycle method. The method annotated remove, all other methods are invoked by the EJB container. Now, if you want, let's see stateful bean with a sample code. First you need to create a bean component and bean client for creating session bean application. This will be our remote interface. And as you can see, we have a remote annotation, and in interface you can see we have three methods. Now, this code snip is the bean class, and as you can see we override methods and we have a stateful map name, after that you will need a bean client. The stateful bean client maybe local, remote, or a web service client. Here, this one is a web-based client and not using dependency injection. We have a form with three radio buttons and a button. As you can see with this form action, we will go to another page. Let's look at operation page codes.

First condition is for withdraw, second one is deposit, and the last one is for balance. Of course, after this JSP we need to doGet method. And the last code snip is our servlet with doGet method. Let's examine the doGet method. First, we have to create our initial context. After that we can define an instance from stateful with remote. And then, we can set attributes to the request session. I hope you now have a better understanding of stateful session beans. In the last video, we will continue talking about the last session bean, singleton session bean. So, I'll see you in the next video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.