Chapter 3. Session Bean Component Contract

Identify correct and incorrect statements or examples about session beans, including conversational state, the SessionBean interface, and create methods.

Session bean is specified at deployment as having one of the following state management modes:

  • STATELESS — the session bean instances contain no conversational state between methods; any instance can be used for any client.

  • STATEFUL — the session bean instances contain conversational state which must be retained across methods and transactions.

The conversational state of a STATEFUL session object is defined as the session bean instance’s field values.

In advanced cases, a session object’s conversational state may contain open resources, such as open sockets and open database cursors. A container cannot retain such open resources when a session bean instance is passivated. A developer of such a session bean must close and open the resources in the ejbPassivate and ejbActivate notifications.

All session beans must implement the SessionBean interface.

public interface SessionBean extends EnterpriseBean {
	void ejbActivate() throws EJBException, RemoteException;
	void ejbPassivate() throws EJBException, RemoteException;
	void ejbRemove() throws EJBException, RemoteException;
	void setSessionContext(SessionContext ctx) throws EJBException, RemoteException;
}

public interface EnterpriseBean extends Serializable {
}
					

A client creates a session bean instance using one of the create<METHOD> methods defined in the session bean’s home interface:

public interface CartHome extends javax.ejb.EJBHome {
	CartRemote create(String customerName, String account)
		throws RemoteException, BadAccountException,
		CreateException;
	CartRemote createLargeCart(String customerName, String account)
		throws RemoteException, BadAccountException,
		CreateException;
}
					
.....
CartRemote cart = cartHome.create(“John”, “7506”);
					
or for LOCAL view:
public interface CartHome extends javax.ejb.EJBLocalHome {
	CartLocal create(String customerName, String account)
		throws BadAccountException, CreateException;
	CartLocal createLargeCart(String customerName, String account)
		throws BadAccountException, CreateException;
}
					
.....
CartLocal cart = cartHome.create(“John”, “7506”);
					

The container creates an instance of a session bean in three steps:

  1. The container calls the bean class’ newInstance() method to create a new session bean instance.

  2. The container calls the setSessionContext(context) method to pass the context object to the instance.

  3. The container calls the instance’s ejbCreate<METHOD>() method whose signature matches the signature of the create<METHOD>() method invoked by the client.

Each session bean class must have at LEAST one ejbCreate<METHOD>() method (NOTE: for Entity Beans create methods are optional).

Invoking create() method by client on home interface of STATELESS session bean will NOT invoke ejbCreate() method on bean object. It only creates new EJB(Local)Object. ejbCreate() on STATELESS session bean is invoked by EJB container when it decides to put object in method-ready pool. For STATELESS session beans container handles creating of instances.

Invoking remove() method by client on home interface of STATELESS session bean will NOT invoke ejbRemove() method on bean object. It does nothing. ejbRemove() on STATELESS session bean is invoked by EJB container when it decides to remove object from method-ready pool. For STATELESS session beans container handles removing of instances.

Invoking create<METHOD>() method by client on home interface of STATEFUL session bean will invoke ejbCreate<METHOD>() method on bean object. More precisely: newInstance(), setSessionContext(context), ejbCreate<METHOD>([ARGS]).

Invoking remove() method by client on home interface of STATEFUL session bean will invoke ejbRemove() method on bean object.

The home interface of a STATELESS session bean must have one (and ONLY one) create() method that takes NO arguments.

The create() method of the remote home interface must return the session bean’s remote interface. The create method of the local home interface must return the session bean’s local interface.

STATELESS session bean CAN have instance variables, but there is no guarantee, that these variables will be accessed by the same client. However, the instance variables of the instance can contain the state across client-invoked method calls. Examples of such states include an open database connection and an object reference to an EJB object.

A STATELESS session bean MUST NOT implement the javax.ejb.SessionSynchronization interface.

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine