Oracle8i Enterprise JavaBeans Developer's Guide and Reference
Release 3 (8.1.7)

Part Number A83725-01


Solution Area



Go to previous page Go to beginning of chapter Go to next page

Implementing the Bean

The bean contains the business logic for your bean. It implements the following methods:

Interface Implemented

Your bean implements the methods within either the SessionBean or EntityBean interface. This example implements the SessionBean interface. Basically, a session bean is used for process oriented beans--those beans that perform tasks to achieve an end. Entity beans are complex remote objects that are organized around persistent data. See "Definition of an Entity Bean" for more information on the differences between the two types of beans.

The session bean implements the javax.ejb.SessionBean interface, which has the following definition:

public interface javax.ejb.SessionBean extends javax.ejb.EnterpriseBean {
      public abstract void ejbActivate(); 
      public abstract void ejbPassivate();
      public abstract void ejbRemove();
      public abstract void setSessionContext(SessionContext ctx);

At a minimum, an EJB must implement the following methods, as specified in the javax.ejb.SessionBean interface:


Implement this as a null method, because it is never called in this release of the EJB server.  


Implement this as a null method, because it is never called in this release of the server.  


A container invokes this method before it ends the life of the session object. This method performs any required clean-up, for example closing external resources such as file handles.  

(SessionContext ctx)

Associate's a bean's instance with context information. The container calls this method after the bean creation. The enterprise bean can store the reference to the context object in an instance variable, for use in transaction management. Beans that manage their own transactions can use the session context to get the transaction context.  

Using setSessionContext

This method is used by a session bean instance to retain a reference to its context. Session beans have session contexts that the container maintains and makes available to the beans. The bean may use the methods in the session context to make callback requests to the container.

The container invokes setSessionContext method, after it first instantiates the bean, to enable the bean to retrieve the session context. The container will never call this method from within a transaction context. If the bean does not save the session context at this point, the bean will never gain access to the session context.

When the container calls this method, it passes the reference of the SessionContext object to the bean. The bean can then store the reference for later use. The following example shows the bean saving the session context in the sessctx variable.

import javax.ejb.*;
import oracle.oas.ejb.*;

public class myBean implements SessionBean {
      SessionContext sessctx;

      void setSessionContext(SessionContext ctx) {
            sessctx = ctx;      // session context is stored in 
                                              // instance variable
      // other methods in the bean

The javax.ejb.SessionContext interface has the following definition:

public interface SessionContext extends javax.ejb.EJBContext {
    public abstract EJBObject getEJBObject();

And the javax.ejb.EJBContext interface has the following definition:

public interface EJBContext {
    public abstract Properties      getEnvironment();
    public abstract UserTransaction getUserTransaction();
    public abstract boolean         getRollbackOnly();
    public abstract void            setRollbackOnly();
    public abstract boolean         isCallerInRole(Identity);  
	// not supported
    public abstract Identity        getCallerIdentity(); // not supported
    public abstract EJBHome        getEJBHome(); 

A bean needs the session context when it wants to perform the operations listed in Table 2-1.

Table 2-1 SessionContext operations
Method  Description 


Get the values of properties for the bean.  


Get a transaction context, which allows you to demarcate transactions programmatically. This is only valid for beans that have been designated transactional.  


Set the current transaction so that it cannot be committed.  


Check whether the current transaction has been marked for rollback only.  


Get the object reference to the bean's corresponding
EJBHome (home interface).  

Bean Implementation Example

The following code implements methods of a session bean called EmployeeBean. The SessionBean interface methods are implemented along with the public methods declared in the remote interface.

The JDBC code opens a default connection, which is the standard way that JDBC code that runs on the Oracle8i server opens a server-side connection. A JDBC prepared statement is used to prepare the query, which has a WHERE clause. Then the setInt method is used to associate the empNumber input parameter for the getEmployee method with the'?' placeholder in the prepared statement query. This is identical to the JDBC code that you would write in a client application.

package employeeServer;

import java.sql.*;
import java.rmi.RemoteException;
import javax.ejb.*;

public class EmployeeBean implements SessionBean {
  SessionContext ctx;

  //implement the bean method, getEmployee
  public EmpRecord getEmployee (int empNumber)
       throws SQLException, RemoteException {

    //create a new employee record
    EmpRecord empRec = new EmpRecord(); 

   //establish a connection to the database using JDBC
    Connection conn =
      new oracle.jdbc.driver.OracleDriver().defaultConnection();

    //retrieve the employee's information from the database
    PreparedStatement ps =
      conn.prepareStatement("select ename, sal from emp where empno = ?");
    ps.setInt(1, empNumber);
    ResultSet rset = ps.executeQuery();
    if (!
        throw new RemoteException("no employee with ID " + empNumber);
    empRec.ename = rset.getString(1);
    empRec.sal = rset.getFloat(2);
    empRec.empno = empNumber;
    return empRec;

  //implement the SessionBean methods: ejbCreate, ejbActivate,
  // ejbPassivate, ejbRemove and setSessionContext

  //implement ejbCreate, which is called by the container when
  //the Home create is invoked by the client.
  public void ejbCreate() throws CreateException, RemoteException {
    //you can do any initialization for the bean at this point.
    //this particular example does not require any initialization or
    //environment variable retrieval.

  //ejbActivate and ejbPassivate are never called in this release.
  //Both methods should be declared, but be null methods.
  public void ejbActivate() {
  public void ejbPassivate() {

  //implement anything that needs to be done before the 
  //bean is destroyed. this would include closing any open
  //resources. however, for this example, no open resources need
  //to be closed. thus, the method is empty.
  public void ejbRemove() {
  //retreive the session context 
  public void setSessionContext(SessionContext ctx) {
    this.ctx = ctx;

Go to previous page
Go to beginning of chapter
Go to next page
Copyright © 1996-2000, Oracle Corporation.

All Rights Reserved.


Solution Area