EJB - Session Beans --- OLD 2.* version (see page for newer api)

 

Characteristics:

  • Represent a "conversation" between a "client" and server.
  • Execute on behalf of a single client.
    • Can not be shared by more than one client at the same time.
    • Also can not be called by same client using mutliple threads.
  • Can be transaction-aware and use security
  • Do not directly represent data in a database
    • but, can access and update data on behalf of client
  • Are usually relatively short-lived.
    • should be removed by client or when EJB container shuts down or crashes.

The SessionBean Class methods


public void setSessionContext(SessionContext c)

container calls to set associated session context

you should write the code to store the reference to the session contain in a class variable so you can access it later.

The session context provides access to runtime session context such as identifying the caller, access or change current transaction state, etc.

public void ejbCreate()

container calls to initialize your session bean instance

put any initialization code here

 


public void ejbRemove()

container invokes this method before it ends life of session

public void ejbActivate()

container calls after it activates ejb

use to restore any resource connections, etc. you need


public void ejbPassivate()

container calls before it passivates ejb

use to close any resources open (e.g. sockets, database connections).

 

Passivation and Activation

  • Only for Stateful Session Beans

  • Passivation =mechanism by which container stores bean's state into a "back store" (file system or database).
    • container starts as soon as number of allocated statefull session beans exceeds a threshold (specified in vendor deployment descriptor) and for some containers can specify max idle time after which will passivate.

      e.g. WebLogic server:

      <stateful-session-cache>
            <max-beans-in-cache>1000</max-beans-in-cache>
      </stateful-session-cache>

    • Serializes all non-transient variables to a persistent store


  • Activation = mechanism by which container activates a passivated instance when bean's client decides to continue interactions with a bean.

 

Stateless Session Beans Stateful Session Beans