Oracle8i Enterprise JavaBeans Developer's Guide and Reference Release 3 (8.1.7) Part Number A83725-01 |
|
The bean contains the business logic for your bean. It implements the following methods:
The bean in the example application consists of one class, EmployeeBean
, that retrieves an employee's information.
SessionBean
or EntityBean
interface.
ejbCreate
methods that corresponds to the create
methods declared in the home interface. The container invokes the ejbCreate
method when the client invokes the corresponding create
method.
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:
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.
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 classEmployeeBean
implementsSessionBean
{ SessionContext ctx; //implement the bean method, getEmployee public EmpRecordgetEmployee
(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 (!rset.next()) throw new RemoteException("no employee with ID " + empNumber); empRec.ename = rset.getString(1); empRec.sal = rset.getFloat(2); empRec.empno = empNumber; ps.close(); 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 voidejbCreate
() 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 voidejbActivate
() { } public voidejbPassivate
() { } //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 voidejbRemove
() { } //retreive the session context public voidsetSessionContext
(SessionContext ctx) { this.ctx = ctx; } }
|
Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
|