Oracle8i Enterprise JavaBeans and CORBA Developer's Guide
Release 8.1.5






Prev  Chap Top Next

Programming Techniques

This section describes some of the programming techniques you can use when developing EJB session beans. The Oracle8i JServer environment offers a very rich development environment for session beans, since you can use all the capabilities provided by the Oracle8i multi-threaded server to manage multiple sessions, use SQLJ to simplify data acquisition and update, and use the UserTransaction interface to manage transactions.

Using SQLJ

The bean developer can use the Oracle8i SQLJ translator to simplify EJBs that access the database using static SQL statements. For example, consider the bean that was implemented in the example earlier in this chapter, in "The Bean Implementation". That implementation required about seven JDBC calls. Here is the same bean, implemented using SQLJ, which requires only two major SQLJ statements:

package employeeServer;

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

package employeeServer;

import employee.EmpRecord;

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

public class EmployeeBean implements SessionBean {
  SessionContext ctx;

  public void ejbCreate() throws CreateException, RemoteException {

  public void ejbActivate() {

  public void ejbPassivate() {

  public void ejbRemove() {

  public void setSessionContext(SessionContext ctx) {
    this.ctx = ctx;

  public EmpRecord query (int empNumber) throws SQLException, RemoteException
    String ename;
    double sal;

    #sql { select ename, sal into :ename, :sal from emp 
                  where empno = :empNumber }; 

    return new EmpRecord (ename, empNumber, sal);

The complete example is available on the distribution CD in the demo.tar file, as sqljimpl in the examples/ejb/basic directory.

Setting a Session Timeout

The session timeout value in the deployment descriptor determines how long a session stays active after the last bean client disconnects. It can be important to keep a session alive in at least two cases:

EJB deployer can set a session timeout value using the SessionTimeout attribute in the bean deployment descriptor (see "The Deployment Descriptor").

Saving an EJB Handle

Using the Oracle8i EJB server, it is possible for a client to connect to a session that was started by another client, and to access a bean in that session. This holds true as long as the second client can authenticate as a valid user of the database.

But to access a session established by another user, the client must have access to a handle for a bean in that session. A client can provide such a handle to another client using the getHandle() method, which returns a bean object reference.

The following code demonstrates one way to get a bean handle, and save it to a file using an output stream. You can also use Java streams to write the bean handle to another reader object.

First, get a reference to a bean in the usual way:

saveHandleHome home =
 (saveHandleHome) ic.lookup("sess_iiop://localhost:2481:ORCL/test/myEmployee");
saveHandle testBean = home.create();

Next, create an object output stream from a file stream:

FileOutputStream fostream = new FileOutputStream(handlefile);
ObjectOutputStream ostream = new ObjectOutputStream(fostream);

Then get the bean handle using getHandle(), and write it to the output stream:


Finally, clean up the streams:


See the complete example in examples/ejb/basic/saveHandle in the demo.tar file on the distribution CD.

EJB as Client

It is possible for an EJB to serve as a client to another EJB. In this case, the client EJB simply looks up the other EJB in the same way the a Java non-EJB client would.

See the example in the examples /ejb/session/clientserverserver directory in the demo file (demo.tar).



Copyright © 1999 Oracle Corporation.

All Rights Reserved.