WAS v8.5 > Develop applications > Develop EJB applications > Develop session beans

Develop stateful session beans

We can create a bean implementation class for a stateful session bean as introduced in the Enterprise JavaBeans™ (EJB) 1.0 specification and significantly simplified by the EJB 3.0 specification. A stateful bean is a type of session bean intended for use by a single client during its lifetime and maintains a conversational state with the client that is calling it.

Make sure that you understand the inheritance rules for each annotation you implement. For example, the @TransactionManagement annotation is coded on the stateful session bean class only. We cannot use the @TransactionManagement annotation in the class that it extends, or any class higher in the class inheritance tree.

Stateful session beans can have the following views: no-interface local view (new in EJB 3.1), business local, business remote, EJB 2.1 local, and EJB2.1 remote client views. One example is a shopping cart where the client adds items to the cart over the course of an on-line shopping session.

The following example shows a basic stateful session bean:

package com.ibm.example;

public interface ShoppingCart {
    void addToCart (Object o);
    Collection getContents();}

package com.ibm.example;

@Stateful
public class ShoppingCartBean implements ShoppingCart {
    private ArrayList contents = new ArrayList();
    
    public void addToCart (Object o) {
        contents.add(o);
    }
    public Collection getContents() {
        return contents;
    }} 
As with other enterprise bean types, we can also declare metadata for stateful session beans in the deployment descriptor rather than using annotations; for example:
<?xml version="1.0"?> <ejb-jar
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"
  version="3.1">   <enterprise-beans>     <ejb-name>ShoppingCartBean</ejb-name>     <business-local>com.ibm.example.ShoppingCart</business-local>     <ejb-class>com.ibm.example.ShoppingCartBean</ejb-class>     <session-type>Stateful</session-type>   </enterprise-beans> </ejb-jar>


Related concepts:

Stateful session bean failover for the EJB container


Related


Use asynchronous beans


Reference:

EJB container system properties


+

Search Tips   |   Advanced Search