Chapter 3. Session Beans

download PDF

3.1. Session Beans

Session beans are enterprise beans that encapsulate a set of related business processes or tasks and are injected into the classes that request them. There are three types of session bean: stateless, stateful, and singleton.

3.2. Stateless Session Beans

Stateless session beans are the simplest yet most widely used type of session bean. They provide business methods to client applications but do not maintain any state between method calls. Each method is a complete task that does not rely on any shared state within that session bean. Because there is no state, the application server is not required to ensure that each method call is performed on the same instance. This makes stateless session beans very efficient and scalable.

3.3. Stateful Session Beans

Stateful session beans are enterprise beans that provide business methods to client applications and maintain conversational state with the client. They should be used for tasks that must be done in several steps, or method calls, each of which relies on the state of the previous step being maintained. The application server ensures that each client receives the same instance of a stateful session bean for each method call.

3.4. Singleton Session Beans

Singleton session beans are session beans that are instantiated once per application and every client request for a singleton bean goes to the same instance. Singleton beans are an implementation of the Singleton Design Pattern as described in the book Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides; published by Addison-Wesley in 1994.

Singleton beans provide the smallest memory footprint of all the session bean types but must be designed as thread-safe. EJB 3.2 provides container-managed concurrency (CMC) to allow developers to implement thread safe singleton beans easily. However singleton beans can also be written using traditional multi-threaded code (bean-managed concurrency or BMC) if CMC does not provide enough flexibility.

3.5. Add Session Beans to a Project in Red Hat JBoss Developer Studio

Red Hat JBoss Developer Studio has several wizards that can be used to quickly create enterprise bean classes. The following procedure shows how to use the Red Hat JBoss Developer Studio wizards to add a session bean to a project.


  • You have a EJB or Dynamic Web Project in Red Hat JBoss Developer Studio to which you want to add one or more session beans.

Add Session Beans to a Project in Red Hat JBoss Developer Studio

  1. Open the project in Red Hat JBoss Developer Studio.
  2. Open the Create EJB 3.x Session Bean wizard.

    To open the Create EJB 3.x Session Bean wizard, navigate to the File menu, select New and then select Session Bean (EJB 3.x).

    Figure 3.1. Create EJB 3.x Session Bean wizard

    Create EJB 3.x Session Bean wizard
  3. Specify following details:

    • Project: Verify the correct project is selected.
    • Source folder: This is the folder that the Java source files will be created in. This should not usually need to be changed.
    • Package: Specify the package that the class belongs to.
    • Class name: Specify the name of the class that will be the session bean.
    • Superclass: The session bean class can inherit from a superclass. Specify that here if your session has a superclass.
    • State type: Specify the state type of the session bean: stateless, stateful or singleton.
    • Business interfaces: By default the No-interface box is checked so no interfaces will be created. Check the boxes for the interfaces you wish to define and adjust the names if necessary.

      Remember that enterprise beans in a web archive (WAR) only support EJB 3.2 Lite and this does not include remote business interfaces.

      Click Next.

  4. You can enter in additional information here to further customize the session bean. It is not required to change any of the information here.

    Items that you can change are:

    • Bean name
    • Mapped name
    • Transaction type (container managed or bean managed)
    • Additional interfaces can be supplied that the bean must implement
    • You can also specify EJB 2.x Home and Component interfaces if required
  5. Click Finish and the new session bean will be created and added to the project. The files for any new business interfaces will also be created if they were specified.

    Figure 3.2. New Session Bean in Red Hat JBoss Developer Studio

    New Session Bean in Red Hat JBoss Developer Studio
Red Hat logoGithubRedditYoutubeTwitter


Try, buy, & sell


About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.