Developing stateful session beans

You 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 that is intended for use by a single client during its lifetime and maintains a conversational state with the client that is calling it.

Before you begin

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. You cannot use the @TransactionManagement annotation in the class that it extends, or any class higher in the class inheritance tree.

About this task

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, you 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>

Procedure

Task topic    

Terms and conditions for information centers | Feedback

Last updated: April 18, 2014 05:01 AM CDT
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=phil&product=was-nd-iseries&topic=tejb_sfsb
File name: tejb_sfsb.html