Stateful Session Bean
Start course

In this course, we will learn the concepts of Java EE 7 with a focus on Enterprise Java Beans (EJB).

Learning Objectives

  • Enterprise Java Beans (EJB)

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to improve Java 7 EE knowledge
  • Java developers


  • Have at least 2 years of Java development experience 

Hello, dear friends. In this video, we examine Session Beans and Stateful Beans. 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 his session. Let's take the example of a shopping cart in an E-commerce website. A customer logs on, chooses a first product, adds it to his 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 only have 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 lifecycle of stateful Bean. The client initiates the lifecycle 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. When 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 passivate stage, the EJB container activates the Bean, calls the method annotated postActivated, if any, and then, moves it to the ready stage.

At the end of the lifecycle, the client invokes a method annotatedRemove and the EJB container calls the method annotatedpreDestroy, if any. The Bean's instance is then ready for garbage collection. Your code controls the invocation of only one lifecycle method, the method annotatedRemove. 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 Bean component and Bean client for creating a Session Bean application. This will be our remote interface. As you can see, we have a remote annotation and an interface. Look, 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 mapped name. After that, you will need a Bean client. The Stateful Bean client maybe local, remote or web service client. Here, this one is 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 withdrawal, second one is deposit, and last one is for balance. Of course, after this JSP, we need a 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 following video, we will continue with the last Session Bean, Singleton Session Bean. 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.

Covered Topics