Oracle8i Enterprise JavaBeans and CORBA Developer's Guide
Release 2 (8.1.6)

A81356-01

Library

Product

Contents

Index

Prev  Chap Top Next

Transaction Examples

clientside

employee.ejb

SessionBean employeeServer.EmployeeBean
{
  BeanHomeName = "test/myEmployee";
  RemoteInterfaceClassName = employee.Employee;
  HomeInterfaceClassName = employee.EmployeeHome;

  AllowedIdentities = { PUBLIC };
  RunAsMode = CLIENT_IDENTITY;
  TransactionAttribute = TX_SUPPORTS;
}

Client.java

import employee.Employee;
import employee.EmployeeHome;
import employee.EmployeeInfo;

import oracle.aurora.jndi.sess_iiop.ServiceCtx;

import oracle.aurora.jts.client.AuroraTransactionService;
import oracle.aurora.jts.util.TS;

import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Hashtable;

public class Client
{
  public static void main (String[] args) throws Exception {
    if (args.length != 4) {
      System.out.println ("usage: Client serviceURL objectName user password");
      System.exit (1);
    }
    String serviceURL = args [0];
    String objectName = args [1];
    String user = args [2];
    String password = args [3];

    // create InitialContext
    Hashtable env = new Hashtable ();
    env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi");
    env.put (Context.SECURITY_PRINCIPAL, user);
    env.put (Context.SECURITY_CREDENTIALS, password);
    env.put (Context.SECURITY_AUTHENTICATION, ServiceCtx.NON_SSL_LOGIN);
    Context ic = new InitialContext (env);

    // initialize the transaction service
    AuroraTransactionService.initialize (ic, serviceURL);

    // get an handle to the employee_home object
    EmployeeHome employee_home = 
      (EmployeeHome)ic.lookup (serviceURL + objectName);

    // get an handle to the remote bean
    Employee employee = employee_home.create ();

    // get Control to the transaction
    TS.getTS ().getCurrent ().begin ();

    // get an info of an employee
    EmployeeInfo info = employee.getEmployee ("SCOTT");
    System.out.println ("Beginning salary = " + info.salary);

    // do work on the info-object
    info.salary += (info.salary * 10) / 100;

    // call update on the server-side
    employee.updateEmployee (info);

    // commit the updated value
    TS.getTS ().getCurrent ().commit (true);

    // start another transaction to get the info again
    // Note: you can also do this before the previous-TX is committed
    //       without starting a new TX (see ../serversideJTS/Client.java)
    TS.getTS ().getCurrent ().begin ();

    EmployeeInfo infoAfterUpdate = employee.getEmployee ("SCOTT");
    System.out.println ("End salary = " + infoAfterUpdate.salary);

    // commit the updated value
    TS.getTS ().getCurrent ().commit (false);
  }
}

employee/EmployeeInfo.java

package employee;

public class EmployeeInfo implements java.io.Serializable {
  public String name = null;
  public int number = 0;
  public double salary = 0;

  public EmployeeInfo (String name, int number, double salary) {
    this.name = name;
    this.number = number;
    this.salary = salary;
  }
}

employee/Employee.java

package employee;

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

import java.sql.SQLException;

public interface Employee extends EJBObject  
{
  public EmployeeInfo getEmployee (String name) 
       throws RemoteException, SQLException;

  public void updateEmployee (EmployeeInfo employee)
       throws RemoteException, SQLException;
}

employee/EmployeeHome.java

package employee;

import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import java.rmi.RemoteException;

public interface EmployeeHome extends EJBHome
{
  public Employee create () throws RemoteException, CreateException;
}

employeeServer/EmployeeBean.sqlj

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
{
  // 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 () {}
}

multiSessions

employee.ejb

SessionBean employeeServer.EmployeeBean
{
  BeanHomeName = "test/myEmployee";
  RemoteInterfaceClassName = employee.Employee;
  HomeInterfaceClassName = employee.EmployeeHome;

  AllowedIdentities = { PUBLIC };
  RunAsMode = CLIENT_IDENTITY;
  TransactionAttribute = TX_SUPPORTS;
}

Client.java

import employee.*;

import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Hashtable;

public class Client
{
  public static void main (String[] args) throws Exception {
    if (args.length != 5) {
      System.out.println ("usage: Client serviceURL objectName " +
			  "user password sessionsCount");
      System.exit (1);
    }
    String serviceURL = args [0];
    String objectName = args [1];
    String user = args [2];
    String password = args [3];
    int sessionCount = Integer.parseInt (args[4]);

    // create InitialContext
    // Note: authentication is done per session in ClientThread
    Hashtable env = new Hashtable ();
    env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi");
    Context ic = new InitialContext (env);

    // invoke different sessions using ClientThread
    for (int i = 0; i < sessionCount; i++) {
      String sessionName = new String (":session" + i);
      ClientThread ct = new ClientThread (ic, serviceURL, objectName, 
					  sessionName, user, password);
      System.out.println ("Starting ClientThread (" + sessionName + ")");
      ct.start ();
    }
  }
}

ClientThread.java

import employee.*;

import oracle.aurora.jts.client.AuroraTransactionService;
import oracle.aurora.jndi.sess_iiop.ServiceCtx;
import oracle.aurora.jndi.sess_iiop.SessionCtx;
import oracle.aurora.AuroraServices.LoginServer;
import oracle.aurora.client.Login;
import oracle.aurora.jts.util.TS;

import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Hashtable;

public class ClientThread extends Thread
{
  private Context ic = null;
  private String serviceURL = null;
  private String objectName = null;
  private String sessionName = null;
  private SessionCtx session = null;

  public ClientThread () {}

  public ClientThread (Context ic, String serviceURL, String objectName, 
		       String sessionName, String user, String password)
  {
    try {
      this.ic = ic;
      ServiceCtx service = (ServiceCtx)ic.lookup (serviceURL);
      this.session = (SessionCtx)service.createSubcontext (sessionName);
      System.out.println ("activating the " + sessionName + " in " + 
			  serviceURL);

      LoginServer login_server = (LoginServer)session.activate ("etc/login");
      Login login = new Login (login_server);
      login.authenticate (user, password, null);
      
      this.serviceURL = serviceURL;
      this.sessionName = sessionName;
      this.objectName = objectName;
    } catch (Exception e) {
      e.printStackTrace ();
    }
  }

  public void run () {
    try {
      this.yield ();

      // Get handle to the TX-Factory
      AuroraTransactionService.initialize (ic, serviceURL + "/" + sessionName);

      // create an instance of an employee object in the session
      EmployeeHome employee_home =
	(EmployeeHome)ic.lookup (serviceURL + "/" + sessionName + 
objectName); Employee employee = employee_home.create (); System.out.println ("employee_home.create () DONE in " + sessionName); EmployeeInfo info = null; // start the transaction TS.getTS ().getCurrent ().begin (); // get the info about an employee // Note: lock is set on the row using 'for update' clause // while select operation info = employee.getEmployeeForUpdate ("SCOTT"); System.out.println ("Beginning salary = " + info.salary + " in " + sessionName); // arbitrarily change the value of the salary, // for e.g. depending on sessionName if (sessionName.endsWith ("0")) { System.out.println ("10% Increase" + sessionName); info.salary += (info.salary * 10) / 100; } else if (sessionName.endsWith ("1")) { System.out.println ("20% Increase" + sessionName); info.salary += (info.salary * 20) / 100; } else { System.out.println ("30% Increase" + sessionName); info.salary += (info.salary * 30) / 100; } // try sleeping this-thread for a while before updating the info // Note: the other threads MUST wait (since selected with // 'for update' clause) this.sleep (2000); // update the infomation in the transaction employee.updateEmployee (info); // get and print the info in the transaction // Note: overwrite info structure // doNOT use 'for update' here (unnecessary locking) info = employee.getEmployee ("SCOTT"); System.out.println ("End salary = " + info.salary + " in " + sessionName); // commit the changes TS.getTS ().getCurrent ().commit (true); } catch (Exception e) { e.printStackTrace (); } } }

employee/Employee.java

package employee;

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

import java.sql.SQLException;

public interface Employee extends EJBObject  
{
  public EmployeeInfo getEmployee (String name) 
       throws RemoteException, SQLException;

  public EmployeeInfo getEmployeeForUpdate (String name) 
       throws RemoteException, SQLException;

  public void updateEmployee (EmployeeInfo employee) 
       throws RemoteException, SQLException;
}

employee/EmployeeHome.java

package employee;

import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import java.rmi.RemoteException;

public interface EmployeeHome extends EJBHome
{
  public Employee create () throws RemoteException, CreateException;
}

employee/EmployeeInfo.java

package employee;

public class EmployeeInfo implements java.io.Serializable {
  public String name = null;
  public int number = 0;
  public double salary = 0;

  public EmployeeInfo (String name, int number, double salary) {
    this.name = name;
    this.number = number;
    this.salary = salary;
  }
}

employeeServer/EmployeeBean.sqlj

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
{
  // Methods of the Employee remote 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 EmployeeInfo getEmployeeForUpdate (String name) 
       throws RemoteException, SQLException
  {
    int empno = 0;
    double salary = 0.0;
    #sql { select empno, sal into :empno, :salary from emp
                  where ename = :name for update };

    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 () {}
}

serversideJTS

Client.java

import employee.Employee;
import employee.EmployeeHome;
import employee.EmployeeInfo;

import oracle.aurora.jndi.sess_iiop.ServiceCtx;

import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Hashtable;

public class Client
{
  public static void main (String[] args) throws Exception {
    if (args.length != 4) {
      System.out.println ("usage: Client serviceURL objectName user password");
      System.exit (1);
    }
    String serviceURL = args [0];
    String objectName = args [1];
    String user = args [2];
    String password = args [3];

    // create InitialContext
    Hashtable env = new Hashtable ();
    env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi");
    env.put (Context.SECURITY_PRINCIPAL, user);
    env.put (Context.SECURITY_CREDENTIALS, password);
    env.put (Context.SECURITY_AUTHENTICATION, ServiceCtx.NON_SSL_LOGIN);
    Context ic = new InitialContext (env);

    // get handle to the employee object
    EmployeeHome employee_home = (EmployeeHome)ic.lookup (serviceURL + 
							  objectName);

    Employee employee = employee_home.create ();

    EmployeeInfo info = employee.getEmployeeForUpdate ("SCOTT");
    System.out.println ("Beginning salary = " + info.salary);

    // do work on the info-object
    info.salary += (info.salary * 10) / 100;

    // call update on the server-side
    employee.updateEmployee (info);

    info = employee.getEmployee ("SCOTT");
    System.out.println ("End salary = " + info.salary);
  }
}

employee.ejb

SessionBean employeeServer.EmployeeBean
{
  BeanHomeName = "test/myEmployee";
  RemoteInterfaceClassName = employee.Employee;
  HomeInterfaceClassName = employee.EmployeeHome;

  AllowedIdentities = { PUBLIC };
  RunAsMode = CLIENT_IDENTITY;
  TransactionAttribute = TX_BEAN_MANAGED;
}

employee/Employee.java

package employee;

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

import java.sql.SQLException;

public interface Employee extends EJBObject  
{
  public EmployeeInfo getEmployee (String name)
       throws RemoteException, SQLException;

  public EmployeeInfo getEmployeeForUpdate (String name)
       throws RemoteException, SQLException;

  public void updateEmployee (EmployeeInfo employee)
       throws RemoteException, SQLException;
}

employee/EmployeeHome.java

package employee;

import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import java.rmi.RemoteException;

public interface EmployeeHome extends EJBHome
{
  public Employee create () throws RemoteException, CreateException;
}

employee/EmployeeInfo.java

package employee;

public class EmployeeInfo implements java.io.Serializable {
  public String name = null;
  public int number = 0;
  public double salary = 0;

  public EmployeeInfo (String name, int number, double salary) {
    this.name = name;
    this.number = number;
    this.salary = salary;
  }
}

employeeServer/EmployeeBean.sqlj

package employeeServer;

import employee.*;

import javax.ejb.SessionBean;
import javax.ejb.CreateException;
import javax.ejb.SessionContext;
import javax.jts.UserTransaction;

import java.rmi.RemoteException;
import java.sql.SQLException;

public class EmployeeBean implements SessionBean
{
  SessionContext ctx;

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

    ctx.getUserTransaction ().commit ();

    return new EmployeeInfo (name, empno, salary);

  }

  public EmployeeInfo getEmployeeForUpdate (String name) 
       throws RemoteException, SQLException 
  {
    ctx.getUserTransaction ().begin ();
    
    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) };

    ctx.getUserTransaction ().commit ();
  }

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

serversideLogging

Client.java

import employee.Employee;
import employee.EmployeeHome;
import employee.EmployeeInfo;

import oracle.aurora.jndi.sess_iiop.ServiceCtx;
import oracle.aurora.jts.client.AuroraTransactionService;
import oracle.aurora.jts.util.TS;

import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Hashtable;

public class Client {
  public static void main (String[] args) throws Exception {
    if (args.length != 4) {
      System.out.println 
	("usage: Client serviceURL objectName user password");
      System.exit (1);
    }
    String serviceURL = args [0];
    String objectName = args [1];
    String user = args [2];
    String password = args [3];

    // create InitialContext
    Hashtable env = new Hashtable ();
    env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi");
    env.put (Context.SECURITY_PRINCIPAL, user);
    env.put (Context.SECURITY_CREDENTIALS, password);
    env.put (Context.SECURITY_AUTHENTICATION, ServiceCtx.NON_SSL_LOGIN);
    Context ic = new InitialContext (env);

    // initialize the transaction service
    AuroraTransactionService.initialize (ic, serviceURL);

    // get Control to the transaction
    TS.getTS ().getCurrent ().begin ();

    // get handle to the employee object
    EmployeeHome employee_home = (EmployeeHome)ic.lookup 
                                                (serviceURL + objectName);

    Employee employee = employee_home.create ();

    EmployeeInfo info = employee.getEmployeeForUpdate ("SCOTT");
    System.out.println ("Beginning salary = " + info.salary);

    // do work on the info-object
    info.salary += (info.salary * 10) / 100;

    // call update on the server-side
    employee.updateEmployee (info);

    // commit the updated value
    TS.getTS ().getCurrent ().commit (true);

    // start another transaction to get the info again
    // Note: you can also do this before the previous-TX is committed
    //       without starting a new TX (see ../serversideJTS/Client.java)
    TS.getTS ().getCurrent ().begin ();

    // re-query for the info object
    EmployeeInfo newInfo = employee.getEmployee ("SCOTT");
    System.out.println ("End salary = " + newInfo.salary);

    // commit the updated value
    TS.getTS ().getCurrent ().commit (false);
  }
}

employee.ejb

// This the generic database work bean template

SessionBean employeeServer.EmployeeBean {
  BeanHomeName = "test/myEmployee";
  RemoteInterfaceClassName = employee.Employee;
  HomeInterfaceClassName = employee.EmployeeHome;

  AllowedIdentities = { PUBLIC };
  RunAsMode = CLIENT_IDENTITY;
  TransactionAttribute = TX_SUPPORTS;
}

log.sql

drop table log_table cascade constraints;

create table log_table (when date, which number, who number, what 
varchar2(2000));
exit

employee/Employee.java

package employee;

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

import java.sql.*;

public interface Employee extends EJBObject  
{
  public EmployeeInfo getEmployee (String name)
       throws RemoteException, SQLException;

  public EmployeeInfo getEmployeeForUpdate (String name) 
       throws RemoteException, SQLException;

  public void updateEmployee (EmployeeInfo employee)
       throws RemoteException, SQLException;
}

employee/EmployeeHome.java

package employee;

import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import java.rmi.RemoteException;

public interface EmployeeHome extends EJBHome
{
  public Employee create () throws RemoteException, CreateException;
}

employee/EmployeeInfo.java

package employee;

import java.rmi.*;

public class EmployeeInfo implements java.io.Serializable {
  public String name = null;
  public int number = 0;
  public double salary = 0;

  public EmployeeInfo (String name, int number, double salary) {
    this.name = name;
    this.number = number;
    this.salary = salary;
  }
}

employeeServer/EmployeeBean.sqlj

package employeeServer;

import employee.*;
import loggingServer.Logging;
import loggingServer.LoggingHome;

import javax.ejb.SessionBean;
import javax.ejb.CreateException;
import javax.ejb.SessionContext;
import javax.jts.UserTransaction;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Hashtable;

public class EmployeeBean implements SessionBean 
{
  SessionContext ctx;
  Logging logServer = null;

  // 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 EmployeeInfo getEmployeeForUpdate (String name) 
       throws RemoteException, SQLException 
  {
    int empno = 0;
    double salary = 0.0;
    logServer.log ("EJB: getEmployeeForUpdate (" + name + ")");
    #sql { select empno, sal into :empno, :salary from emp
                  where ename = :name for update };
    return new EmployeeInfo (name, empno, salary);
  }

  public void updateEmployee (EmployeeInfo employee) 
       throws RemoteException, SQLException
  {
    logServer.log ("EJB: updateEmployee (" + employee.name + ")");
    #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 
  {
    try {
      // create InitialContext
      Hashtable env = new Hashtable ();
      env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi");
      Context ic = new InitialContext (env);

      // Now, to create the loggingBean
      String objectName = new String ("/test/loggingService");
      LoggingHome logBean_home = 
	(LoggingHome)ic.lookup ("sess_iiop://thisServer" + objectName);

      logServer = logBean_home.create ();
    } catch (NamingException e) {
      e.printStackTrace ();
    }

    try {
      logServer.log ("EJB: Create Employee");
    } catch (SQLException e) {
      e.printStackTrace ();
    }
  }
  public void ejbRemove () {}
  public void setSessionContext (SessionContext ctx) {
    this.ctx = ctx;
  }
  public void ejbActivate () {}
  public void ejbPassivate () {}
}

loggingServer/Logging.java

package loggingServer;

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

import java.sql.*;

public interface Logging extends EJBObject {
  public void log (String message) throws RemoteException, SQLException;
}

loggingServer/LoggingBean.sqlj

package loggingServer;

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

import java.sql.*;

import oracle.aurora.rdbms.DbmsJava;
import oracle.aurora.rdbms.Schema;

public class LoggingBean implements SessionBean {
  SessionContext ctx;

  public void log (String message) throws RemoteException, SQLException {
    int ownerNumber = Schema.currentSchema ().ownerNumber ();
    // System.out.println ("ownerNumber = " + ownerNumber);

    // get the session-id
    int sessID = DbmsJava.sessionID (DbmsJava.USER_SESSION);

    #sql { insert into log_table (who, which, when, what) values 
                        (:ownerNumber, :sessID, sysdate, :message) };
  }

  public void ejbCreate () throws RemoteException, CreateException {}
  public void ejbRemove () {}
  public void setSessionContext (SessionContext ctxArg) {
    ctx = ctxArg;
  }
  public void ejbActivate () {}
  public void ejbPassivate () {}
}

loggingServer/LoggingHome.Java

package loggingServer;

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

public interface LoggingHome extends EJBHome {
  public Logging create () throws RemoteException, CreateException;
}

loggingServer/logBean.ejb

// This the generic database work bean template

SessionBean loggingServer.LoggingBean {
  BeanHomeName = "test/loggingService";
  RemoteInterfaceClassName = loggingServer.Logging;
  HomeInterfaceClassName = loggingServer.LoggingHome;

  TransactionAttribute = TX_REQUIRES_NEW;
  RunAsMode = CLIENT_IDENTITY;      
  AllowedIdentities = { PUBLIC };

  EnvironmentProperties {
    prop1 = value1;
    prop2 = "value two";
  }
}



Prev

Top

Next
Oracle
Copyright © 1999 Oracle Corporation.

All Rights Reserved.

Library

Product

Contents

Index