Types of Enterprise Beans
Session Beans
- associated with one EJB Client
- created and destroyed by the particular EJB Client
- nonpersistant, do not survive a server shutdown/crash.
- obviously used to model some service
Two types of session beans exist: stateful and stateless
The class implementing either stateless or stateful session bean
implements the SessionBean interface:
public interface SessionBean extends EnterpriseBean {
void setSessionContext(SessionContext sessionContext)
throws EJBException, RemoteException;
// called by container to provide the bean a "session" context
// to allow it to access the runtime information
void ejbRemove() throws EJBException, RemoteException;
// the container notifies the bean that the bean instance
// is to be removed
void ejbActivate() throws EJBException, RemoteException;
// notifies the stateful bean that it has been activated
// (so it may eventually reacquire system resources that
// were released when the bean was previously passivated)
void ejbPassivate() throws EJBException, RemoteException;
// notifies the stateful bean that it is about to be passivated
// by the container
}
Entity Beans
- always have states
- may be shared by multiple EJB Clients
- their states can be persisted and stored across multiple invocations
- survive server shutdown/crash
- encapsulate in their object reference a unique ID that points to their state
The class implementing entity bean implements the EntityBean interface:
public interface EntityBean extends EnterpriseBean {
public void setEntityContext(EntityContext ctx);
// called by container to pass a reference to the EntityContext interface
// to the bean instance. The entity context interface provides methods
// to access properties of the runtime context
public void unsetEntityContext();
// called by the container before it terminates the life of the current
// instance of the entity bean. The bean can free resources that have
// been allocated during the setEntityContext() call (if any)
public void ejbRemove();
// removes the database entry or entries associated with this particular
// entity bean. The container calls this method when a client invokes
// a remove() method
public void ejbActivate();
// notifies an entity bean that it has been activated. The container
// invokes this method on the instance selected from the pool
// of available instances and assigned to a specific entity object
// identity. When activated, the entity bean instance has
// the opportunity to acquire additional resources it might need
public void ejbPassivate();
// notifies an entity bean that it is about to be deactivated
// -that is, the instance is about to be disassociated from an entity
// object identity and returned to the pool of available instances.
// The instance has the chance to release any resources allocated
// during the ejbActive() method which it might not want to hold
public void ejbLoad();
// refreshes the data the entity object represents from the database
public void ejbStore();
// stores the data the entity object represents in the database
}
|