Getting Started with Session Beans

This topic provides an overview of session bean development. It contains the following sections:


What are Session Beans?

Session beans are used to execute business tasks for a client on the server. A session bean typically implements a certain kind of activity, such as ordering products or signing up for courses, and in executing the business rules typically invokes entity beans. For instance, ordering products is likely to involve stored information about products, customers, and credit cards, while signing up for courses is likely going to require invoking entity beans representing students and courses.

Developing Session Beans with Workshop

In Workshop, you develop a session bean by creating a class that extends weblogic.ejb.GenericSessionBean and implements javax.ejb.SessionBean. You annotate this class with @Session, @JndiName, and @FileGeneration annotations (and others, as needed) that specify EJB characteristics.

You can get started easily in the IDE by using the WebLogic Session Bean template. When you use the template, the IDE generates code such as the following:

/**
 * GenericSessionBean subclass automatically generated by
 * Workshop.
 * 
 * Please complete the ejbCreate() method, add all desired business
 * methods and review the Session, JndiName and FileGeneration annotations to
 * ensure the settings match your intended use.
 */
@Session(ejbName = "MySessionBean")
@JndiName(remote = "ejb.MySessionBeanRemoteHome")
@FileGeneration(remoteClass = Constants.Bool.TRUE, 
        remoteHome = Constants.Bool.TRUE, 
        localClass = Constants.Bool.FALSE, 
        localHome = Constants.Bool.FALSE)
public class MySessionBean 
    extends GenericSessionBean 
    implements SessionBean {
    
    private static final long serialVersionUID = 1L;

    /*
     * (non-Javadoc)
     * 
     * @see weblogic.ejb.GenericSessionBean#ejbCreate()
     */
    public void ejbCreate() {
        // IMPORTANT: Add your code here
    }

    // IMPORTANT: Add business methods
}

Note: To use the WebLogic Session Bean template, in the Project Explorer right-click the package that will contain the bean, select New > Other, expand EJB, then click WebLogic Session Bean.

The code includes typical values for the commonly used class-level annotation attributes. The idea is to provide a starting place for your own code — for you to rewrite it with your code for methods for business logic, and so on.

Workshop uses these annotations to generate the interfaces and descriptor files that are required for EJB session beans. The following sections describe these session bean pieces and characteristics.

Stateful and Stateless

There are two types of session beans: stateful and stateless. A stateful session bean maintains conversational state. In other words, a stateful session bean remembers the calling client application from one method to the next. For a stateful session bean, the results produced by one method might be co-dependent on the results of its prior methods invoked by the same client. A stateful session bean maintains this conversation with the client until the conversation times out or the client explicitly ends the conversation by invoking the bean's remove method.

In contrast, a stateless session bean does not maintain any conversational state; that is, it does not remember which client invoked one of its methods, and does not maintain an internal state between methods. Each session bean method is independent, and the only client input is the data passed in its parameters.

Note: When creating a new EJB, a stateless bean is created by default.

Stateful session beans are tied to a particular client for the duration of the conversation, while stateless session beans are only tied to a particular client for the duration of a method execution. After method execution completes, a stateless session bean is ready to serve another client application. Consequently, a small number of stateless session beans can be used to serve a large number of client applications. Stateless session beans tend to be more commonly preferred over stateful session beans for this reason. When the client application is a page flow or a conversational web service, conversational state is remembered by the client application itself, making it possible to use a stateless session bean while maintaining a continuous session with the user of the client application.

In Workshop, you specify whether a session bean is stateful or stateless by using the @Session annotation's type attribute.

Home and Business Interfaces

Like an entity bean, a session bean can have four different interfaces, called the local home interface, the local business interface (or simply, the local interface), the remote home interface, and the remote business interface (or simply, the remote interface). The local interfaces define the bean's methods that can be used by other EJBs, EJB controls, web services and page flows defined within the same application. That is, if you define a session bean and only plan to use it within that application, you can use local interfaces. In contrast, the remote interfaces define the bean's methods that be invoked by EJBs, EJB controls, web services and page flows defined in other applications.

You can view and set the interfaces defined for a session bean through the Properties view. With your cursor in the bean class's declaration, scroll to where the FileGeneration annotation is listed. The localHomeName and remoteHomeName attributes specify the local and remote interface names. As these are set, corresponding @FileGeneration Annotation values should be visible in your source code.

A session bean's (remote or local) home interface contains the create methods used to obtain a reference to the bean instance. Its (remote or local) business interface contains the component methods that are used to encapsulate a particular piece of business logic.

Create Methods

For a stateless session bean, you must define exactly one ejbCreate() method with no parameters. This method must be invoked to obtain to a reference to a session bean instance. Once you have obtained a reference, you can invoke the session bean's component methods. If you call a stateless session bean via an EJB control, you do not need to call the create method explicitly; the EJB control will create a reference for you when you call a component method.

A stateful session bean must have at least one ejbCreate method and, like entity beans, can have multiple ejbCreate methods. One of these methods must be invoked to obtain a reference to the session bean instance before you can invoke the session bean's component methods. If you call a stateful session bean via an EJB control, you must first call (one of) its create methods to obtain a reference.

Unlike stateless session beans, when you can call a stateful session bean's create method to obtain a reference and subsequently invoke several component methods, each method is guaranteed to be handled by the same bean instance on the server. For more information, see Life Cycle of a Session Bean.

Component Methods

Component methods are the business methods that are invoked on a session bean instance. A simple example of a business method is reserveTickets(customer, movieName, date), which would be used to reserve tickets for a movie.

In principle there is no difference between component methods for a stateful and a stateless session bean. However, the component methods of a stateless session bean must be passed all the necessary data to execute business logic as parameters, while this is not necessary for the component methods of a stateful session bean. For instance, for a stateful session bean the component method reserveTickets can be used to make ticket reservations for a movie after the component method setCustomer(customer) is called to set the customer data, setMovie(name) is called to make the movie selection, and setDate(date) is called to set the movie time. For a stateless session bean, these parameters must be passed to the component method making the actual reservations each time, as in reserveTickets(customer, movieName, date).

Other Methods

A session bean has several predefined methods, as well as a number of callback methods, invoked by the EJB container during certain operations, that a session bean must implement. In WebLogic these callback methods are by default automatically implemented. In many cases you will find it unnecessary to use these methods. To learn more about these methods, see Defining a Session Bean and Life Cycle of a Session Bean.


Still need help? Post a question on the Workshop newsgroup.