What is a stateless session bean?

Richard Monson-Haefel

A stateless session bean is an enterprise bean (EJB component) that provides a stateless service to the client. Conceptually, the business methods on a stateless session bean are similar to procedural applications or static methods; there is no instance state, so all the data needed to execute the method is provided by the method arguments.

The stateless session bean is an EJB component that implements the javax.ejb.SessionBean interface and is deployed with the declarative attribute "stateless". Stateless session beans are called "stateless" because they do not maintain conversational state specific to a client session. In other words, the instance fields in a stateless session bean do not maintain data relative to a client session. This makes stateless session beans very lightweight and fast, but also limits their behavior.

Stateless session beans are usually developed as a set of related and simple services. Think of a session bean as a functional API where each business method represents an isolated independent service. An example is a CreditService bean that provides methods for making charges against different types of credit cards (MC, Visa, Discovery, etc).

public class CreditService implements javax.ejb.SessionBean {

    public Charge charge (String accountToCredit, CreditCard card, double amount)
    throws ValidationException, RemoteException{
               // attempt to obtain Merchant bean representing the retailer making the charge.
                   MerchantHome mrchntHome = ... get VendorHome reference
                   Merchant merchant = mrchntHome.findByPrimaryKey(vendorToCredit);      
               }catch(ObjectNotFoundException onfe){
                         throw new ValidationException("Invalid merchant account number");

               // attempt to create a Charge bean based on the vendor, amount, and credit card info
                   ChargeHome chgHome = ... get ChargeHome reference
                   Charge charge =  chgHome.create(merchant, card, amount);
                    return charge;
              }catch(CreateException ce){
                        throw new ValidationException(ce.getMessage());

In the above example the CreditService bean method charge( ) is completely independent of the bean state or any other methods. The charge( ) method is a stateless service and the CreditService bean is a stateless bean. The charge( ) method uses two other bean types to process the charge. Its normal for stateless beans to use other beans but they can also access the database directly or even other resources like proprietary connections. Below is a list the beans used in this example.

Bean NameBean Type
CreditServiceStateless Session

An example of how the CreditService bean could be used by a client application or bean is shown in the FAQ entry Java:API:EJB:SessionBean:Stateful:What is a stateful session bean?

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.