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. Enterpise JavaBeans
Applets/Servlets vs. Enterpise JavaBeans
J2EE TECHNOLOGIES ENTERPRISE JAVABEANS OVERVIEW TYPES OF ENTERPRISE JAVABEANS EJB CONTAINER/SERVER

          EJB Container/Server is responsible for managing the Enerprise JavaBeans. The container interacts with beans using bean's required management callback 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:

ENTERPRIZE JAVABEAN 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 donot survive application sserver crashes. Session Bean may perform database operations but the Session Bean itself is not a persistent object.
  • STATELESS SESSION BEAN STATEFUL SESSION BEAN OTHER FUNCTIONALITIES IN SESSION BEAN ENTITY BEAN BEAN MANAGED ENTITY BEAN CONTAINER MANAGED ENTITY BEAN TRANSACTIONS IN EJB PRAGRAMMATIC 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;
                           }
                     }
            }
     

    EJB WITH SERVLETS

    CORBA AND RMI-IIOP REFERENCES
    <BACK>