Enterprise JavaBeans in a Nutshell

Compiled by Channu Kambalyal

PREFACE
There are few good books available in market that give good description of EJB along with examples. The EJB specifications are also available on the web for the public. The short notes prepared here out of all these resources. These notes form as a quick reference for new EJB developers. For more details and sample codes refer to the references given below and also the EJB products' guides.
INTRODUCTION
JavaBeans vs. Enterprise JavaBeans
Applets/Servlets vs. Enterprise JavaBeans
J2EE TECHNOLOGIES ENTERPRISE JAVABEANS OVERVIEW TYPES OF ENTERPRISE JAVABEANS EJB CONTAINER/SERVER
The EJB Container/Server is responsible for managing the Enterprise JavaBeans. The container interacts with beans using the bean's required management call-back methods. These methods are invoked only by the Container and not the client applications that use them. The Container alerts the beans about middleware events, makes them available for clients to invoke remotely, perform transaction co-ordination, manage bean's life cycle and other tasks. Overview of the features of Container are as follows:
ENTERPRISE JAVA BEAN
Session Bean Vs. Entity Bean
SESSION BEAN
  • Session Bean represent work performed for a client and represent business processes involving logic, algorithms, or workflow. Session Beans are relatively short-lived component.  The Container is empowered to destroy session beans if clients time out. Session beans are in memory objects and do not survive application server crashes. Session Bean may perform database operations but the Session Bean itself is not a persistent object.
  • Conversational vs. Non conversational Beans - Conversation means conversation between a client and a bean. The conversation may be over a single method or multiple methods. A Stateless Session bean is one that holds conversation over one method call. No state information is stored between method calls. A Stateful Session Bean is one that holds conversation over more than one method call and stores the state of the of that conversation for that client.
  • All Session Beans' Methods are Serialized: The container guarantees that no other clients are using that instance. The container automatically makes the clients to line up one by one to use a bean instance. However this is in no way a performance bottleneck as container can provide other instances of the bean to service multiple simultaneous clients.
  • Because client requests are serialized, you do not need to code your beans as  re-entrant (thread-safe).
  • A Session Bean should implement the Interface SessionBean shown below:
  • public interface javax.ejb.SessionBean    implements     javax.ejb.EnterpriseBean
    {
         public    abstract    void    getSessionContext(SessionContext ctx)    throws
                                                                                                               ava.rmi.RemoteException;
         public    abstract    void    ejbPassivate()      throws    java.rmi.RemoteException;
         public    abstract    void     ejbActivate()       throws    java.rmi.RemoteException;
         public    abstract    void    ejbRemove()        throws    java.rmi.RemoteException;
    }
  • EJBContext: EJBContext is used in determining information about EJBean's current status at runtime. Information may be about HomeObject or EJBObject, transactions, security and environment properties. The SessionContext used in a Session Bean and the EntityContext used in Entity Bean extend the Interface EJBContext:
  • SessionContext: A SessionContext is a beans gateway to interact with container. Your bean can query the container to get information about the current transactional state and current security state.
  • public interface javax.ejb.SessionContext extends javax.ejb.EJBContext
    {
        public    javax.ejb.EJBObject    getEJBObject();
    }
    STATELESS SESSION BEAN STATEFUL SESSION BEAN OTHER FUNCTIONALITIES IN SESSION BEAN ENTITY BEAN BEAN MANAGED PERSISTENT ENTITY BEAN CONTAINER MANAGED PERSISTENT ENTITY BEAN TRANSACTIONS IN EJB PROGRAMMATIC TRANSACTIONS         public void deposit(double amt) throws AccountException
            {
                    javax.transaction.UserTransaction userTran = ctx.getUserTransaction();
                    userTran.begin();

                    balance += amt;

                    try {
                            userTran.commit();
                    }
                    catch(Exception e) {
                            throw new AccountException("Deposit failed due to " + e.toString());
                    }
            }

    DECLARATIVE TRANSACTIONS

              public interface javax.ejb.SessionSynchronization
             {
                    public void afterBegin();
                    public void beforeCompletion();
                    public void afterCompletion(boolean);
            }

            The session that is required to be in transaction should impement the interface  javax.ejb.SessionSynchronization as follows:

            public class MySessionBean implements SessionBean, SessionSynchronization
            {
                    private  SessionContext    ctx;
                    public    int                        val;

                    public void ejbCreate(int val) throws createException  { this.val = val;  }
                    public int getVal() { return ++val; }
                    public void ejbRemove() {  }
                    public void ejbActivate() {  }
                    public void ejbPassivate() {  }
                    public void setSessionContext(SessionContext ctx) {  }
                    public void afterBegin()  {
                             System.out.println("Transaction Begun!");
                    }
                    public void beforeCompletion()  {
                              System.out.println("Transaction is about to be completed (with Commit or Abort)!");
                    }
                    public void afterCompletion(boolean b)  {
                          if(b == true)  {
                                System.out.println("Transaction Committed!");
                          }
                          if(b == false)  {
                               System.out.println("Transaction Aborted!");
                               --val;
                           }
                     }
            }

    CORBA AND RMI-IIOP

    REFERENCES
    <BACK>