Oracle8i Enterprise JavaBeans Developer's Guide and Reference Release 3 (8.1.7) Part Number A83725-01 |
Library | Service | Contents | Index

## Using the Session Synchronization Interface

An EJB that is a session bean can optionally implement the session synchronization interface, to be notified by the container of the transactional state of the bean. The following methods are specified in the `javax.ejb.SessionSynchronization` interface:

#### afterBegin

    public abstract void afterBegin() throws RemoteException

The `afterBegin()` method notifies a session Bean instance that a new transaction has started, and that the subsequent methods on the instance are invoked in the context of the transaction.

A bean can use this method to read data from a database and cache the data in the bean's fields.

This method executes in the proper transaction context.

#### beforeCompletion

    public abstract void beforeCompletion() throws RemoteException

The container calls the `beforeCompletion()` method to notify a session bean that a transaction is about to be committed. You can implement this method to, for example, write any cached data to the database.

#### afterCompletion

    public abstract void afterCompletion(boolean committed) throws RemoteException

The container calls `afterCompletion()` to notify a session bean that a transaction commit protocol has completed. The parameter tells the bean whether the transaction has been committed or rolled back.

This method executes with no transaction context.

#### Example 7-13 SessionSynch Example

In order for the container to invoke your bean implementation before and after every transaction, your bean must implement the SessionSynch interface.

    package employeeServer;

    import employee.*;

    import javax.ejb.SessionBean;
    import javax.ejb.CreateException;
    import javax.ejb.SessionContext;
    import java.rmi.RemoteException;

    import java.sql.SQLException;

    public class EmployeeBean implements SessionBean implements SessionSynch
    {
      // Methods of the Employee interface
      public EmployeeInfo getEmployee (String name)
           throws RemoteException, SQLException
      {
        int empno = 0;
        double salary = 0.0;
        #sql { select empno, sal into :empno, :salary from emp
                      where ename = :name };

        return new EmployeeInfo (name, empno, salary);
      }

      public void updateEmployee (EmployeeInfo employee)
           throws RemoteException, SQLException
      {
         #sql { update emp set ename = :(employee.name),
                       sal = :(employee.salary)
                       where empno = :(employee.number) };
        return;
      }

      // Methods of the SessionBean
      public void ejbCreate () throws RemoteException, CreateException {}
      public void ejbRemove () {}
      public void setSessionContext (SessionContext ctx) {}
      public void ejbActivate () {}
      public void ejbPassivate () {}

      public void beforeBegin()
      {
        ... perform work ...
      }
      public void afterCompletion()
      {
        ... perform work ...

      }
    }

| Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
Library | Service | Contents | Index